合并文件基于部分匹配的文件名

2024-04-20 14:03:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测量因变量和自变量(比如说,设备测量的电流和电压),测量设置将为正测量值和负测量值提供一个单独的文件。每个文件都是一个excel文件,有2列,电压和电流各一列。我可以给它们取任何我想要的名字,所以我把它们命名为device1\u pos,device1\u neg,device2\u pos,device2\u neg,device3\u pos,device3\u neg等等。另外,我可以对给定的设备进行重复测量,因此我将其命名为device1\u pos\u meas2,device1\u neg\u meas2。收集完所有数据后,我想将给定设备的正测量值和负测量值合并到一个文件中。因此,我想有像device1(结合device1_pos和device1_neg,但我会结合在一个单独的文件,如device1_meas2相同的设备第二次测量)等文件的每个设备数据。你知道吗

有没有一种方法可以用python或shell脚本自动化这个过程?如果有一个更聪明的方法,我可以命名我的文件,使过程更容易,这将是一个有益的建议,以及。你知道吗

添加更多信息到我的第一个问题-我想我可以合并2个文件如下所示。我正在连接这两个文件,但由于我不想要标题和行索引,我把它逐行读取到一个csv文件中(不是最有效的方法,但我可以找出一个)。你知道吗

import os
import pandas as pd
from xlrd import open_workbook
import xlwt

你知道吗操作系统chdir('C:\Users\fg7xmx\Documents\Projects\ESD\TestBench\Measurement\100616')

path=os.getcwd()
file_pos=raw_input("Enter pos data file:")
file_neg=raw_input("Enter neg data file:")
file_allData=raw_input("Enter all data file name:")
file_csv=raw_input("Enter csv file name:")
file1=pd.read_excel(file_pos)
file2=pd.read_excel(file_neg)
file3=pd.concat([file1,file2],axis=0)
file3.to_excel(file_allData)
wb=open_workbook(file_allData)
for sheet in wb.sheets():
    workbook=xlwt.Workbook()
    newSheet = workbook.add_sheet('TLP_IV')
    for row in range(sheet.nrows):
        if row==0: continue
        for col in range(sheet.ncols):
            if col==0: continue
            newSheet.write(row-1,col-1,sheet.cell_value(row,col))
workbook.save(file_csv)

但是,如您所见,我手动输入每个文件名,这对于大量文件来说是不合理的。我的实际文件名看起来像

Mod5\U pin10\U pin8\U位置\U dev1\U 10-06-16\U 10'01'21_上午.xls你知道吗

我知道使用正则表达式我可以匹配给定的模式,但在这里,我需要将具有相同mod编号、相同pin编号、相同测量域(pos或neg)、相同dev编号、相同日期戳和忽略时间戳的文件组合在一起。我不知道我能用什么命令来分组。你知道吗


Tags: 文件csvposimportinputrawexcelfile
1条回答
网友
1楼 · 发布于 2024-04-20 14:03:00

我是这样做的

groups= defaultdict(list)
group_sweep=defaultdict(list)
for filename in os.listdir('C:\\Users\\TLP_IV'):
    basename, extension = os.path.splitext(filename)
    mod, name, pin1, pin2, sweep, dev, meas, date, time, hour=basename.split('_')
    groups[mod, name, pin1, pin2, dev, meas, date].append(filename)
    group_sweep[sweep].append(filename)

我确保用相同的命名约定命名每个文件,并用“\”作为分隔符。一旦我可以创建一个包含各种属性列表的字典,我就可以按键分组并读取给定键的值,如下所示

for keys,values in group_sweep.items():
    i=""
    for key in keys:
        if key=='n':
            for value in values:
                print value

相关问题 更多 >