python 從txt文件中提取數據保存到 xlxs 文件中


1、python 時間的轉換和大小比較

2、寫數據到 xlsx 中

 

'''
@description: 獲取指定的文件中指定的數據
@param {*}
@return {*}
@author: wanghao
'''
'''
程序所在路徑必須和文件所在路徑在同一個文件夾
驗證方法是:打開vscode 的終端:輸入 pwd,程序的名字和想要提取文件的名字都能看到即表示在同一個文件夾里

版本可以修改的東西為:提取文件的文件名,想要提取數據的列名,想要保持的文件名
其他部分不建議修改
'''

import xlwt
import openpyxl
import os
import time
import datetime
from xlwt.Worksheet import Worksheet

def readData(fileName):    

    fopen = open(fileName, 'r')
    lines = fopen.readlines()

    return lines

def getColNum(line, get_col):
    get_col_num = []
    index = -1
    col_info = line.split(',')
    for col in col_info:
        index = index + 1
        tmp = col.strip()
        if get_col[0] == tmp:
            get_col_num.append(index)
        elif get_col[1] == tmp:
            get_col_num.append(index)
        
    return get_col_num

if __name__ == '__main__':

    # 從txt文檔中讀取數據
    txtFileName = '1.txt'                    #改文件名
    lines = readData(txtFileName)

    get_col = ['[OCS]d_ppm', '[CO2]d_ppm_sd']    #改想要提取的數據列
    get_col_num = []    

    # 寫輸入到 xls 中
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = '數據處理'
    data = ['  Time'] + get_col
    ws.append(data)

    # 按行處理數據
    i = -1
    for line in lines:
        i = i + 1
        if 0 == i:
            continue
        elif 1 == i:
            get_col_num = getColNum(line, get_col)
            print(get_col_num)
        elif 2 < i:
            col_info = line.split(',')
            tmp = [col_info[0]]
            for j in range(len(get_col_num)):
                tmp.append(col_info[get_col_num[j]])
            ws.append(tmp)
    wb.save('113.xlsx')        #改保存的文件名

 

 

v2.0:添加時間得篩選

'''
@description: 獲取指定的文件中指定的數據
@param {*}
@return {*}
@author: wanghao
'''
'''
程序所在路徑必須和文件所在路徑在同一個文件夾
驗證方法是:打開vscode 的終端:輸入 pwd,程序的名字和想要提取文件的名字都能看到即表示在同一個文件夾里

版本可以修改的東西為:
1、提取文件的文件名
2、想要提取數據的列名
3、想要保持的文件名
4、提取數據的起止時間段需要去修改
其他部分不建議修改
'''

import datetime
import os
import time

import openpyxl
import xlwt
from xlwt.Worksheet import Worksheet


# 打開指定的txt文件
def readData(fileName):    
    fopen = open(fileName, 'r')
    lines = fopen.readlines()
    return lines

# 獲取指定時間的時間戳
def get_time_stamp(valid_time):
    dd = datetime.datetime.strptime(valid_time, '%m/%d/%Y %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S')
    ts = int(time.mktime(time.strptime(dd, '%Y-%m-%d %H:%M:%S')))
    return ts

# 獲取指定列的數據的列數
def getColNum(line, get_col):
    get_col_num = []
    index = -1
    col_info = line.split(',')
    for col in col_info:
        index = index + 1
        tmp = col.strip()
        if get_col[0] == tmp:
            get_col_num.append(index)
        elif get_col[1] == tmp:
            get_col_num.append(index)
        
    return get_col_num

if __name__ == '__main__':

    # 從txt文檔中讀取數據
    txtFileName = '1.txt'                    #改文件名
    lines = readData(txtFileName)

    get_col = ['[OCS]d_ppm', '[CO2]d_ppm_sd']    #改想要提取的數據列
    get_col_num = []    

    # 寫輸入到 xls 中
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = '數據處理'
    data = ['  Time'] + get_col
    ws.append(data)

    start_time = '06/24/2021 15:47:07.172'    #提取數據的起止時間需要修改
    end_time = '06/24/2021 15:47:25.073'

    start_time_stamp = get_time_stamp(start_time)
    end_time_stamp = get_time_stamp(end_time)

    # 按行處理數據
    i = -1
    for line in lines:
        i = i + 1
        if 0 == i:
            continue
        elif 1 == i:
            get_col_num = getColNum(line, get_col)
            print(get_col_num)
        elif 2 < i:
            col_info = line.split(',')
            cur_stamp = get_time_stamp(col_info[0].strip())
            if cur_stamp - start_time_stamp < 0:
                continue
            if cur_stamp - end_time_stamp > 0:
                break
            tmp = [col_info[0]]
            for j in range(len(get_col_num)):
                tmp.append(col_info[get_col_num[j]])
            ws.append(tmp)
    wb.save('114.xlsx')        #改數據保存的文件名

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM