** 编辑 **
我刚刚意识到这不会产生您所追求的确切格式。保留它以防其他人发现它有用
** 编辑 **
您正在查看的数据看起来像是使用键值对的自定义格式。我不知道你是否想使用 csv 模块来读取这些文件。 (虽然在编写输出 csv 文件时非常有用)
格式如下:
不同的行可能有不同的参数(无法从您提供的非常小的数据 sn-p 中分辨出来)。看起来您在文件的前面添加了“时间、参数、值”,这就是我们看到奇怪的“值00:00”条目的原因。我认为您的意思是在 Value 之后添加一个换行符。
我制作了一个包含一些数据的虚拟文件,因为我认为你拥有它:
00:00, RecordID,5,Age,73
00:42,PaCO2,3400:42,PaO2,34401:11
01:11,SysABP,10501:11,Temp,35.201:11
在这里,我们希望输出 csv 文件具有的唯一列名是
RecordID, Age, PaCO2, PaO2, SysABP, Temp
我们需要遍历文件以发现所有这些。找到它们后,我们可以创建一个带有适当列的 csv.DictWriter。然后我们再次循环输入文件,将我们看到的所有内容写入 dict。
我在上面创建的虚拟文件上成功测试了这个脚本。希望从脚本中的 cmets 可以清楚地看出发生了什么。
import csv
def txt_to_csv(input_filenames):
for input_filename in input_filenames:
column_names = set()
output_filename = input_filename[:-4] + '.csv'
with open(input_filename, 'rb') as in_txt:
# figure out which column names are in the file on at least one line
for line in in_txt:
# get a list of parameters that were split by comma in the input txt file
params = line.strip().split(",")
# lines[1::2] slices out every other entry starting with the first column name
# we or the entries into the set to keep our memory footprint small by only
# storing one copy of each unique column name
# we strip each entry of any extra whitespace while doing a set comprehension.
column_names |= set(params[1::2])
# notice that we always skip the first column with the timestamp by starting at 1
# strip off any extra whitespace in column names
column_names = {x.strip() for x in column_names}
# add in missing timestamp column to the column names
column_names.add('timestamp')
# sort column names and convert python3 strings to bytes as required by csv module
sorted_column_names = sorted(column_names)
# bring the pointer back to the beginning of the file
in_txt.seek(0, 0)
# open a csv file and start writing the output
with open(output_filename, 'wb') as out_csv:
writer = csv.DictWriter(out_csv, sorted_column_names, dialect='excel')
# write column names
writer.writeheader()
for line in in_txt:
# create a list of values for this line
params = [x.strip() for x in line.strip().split(",")]
# turn key value pairs into dictionary
row_dict = dict(zip(params[1::2], params[2::2]))
# write timestamp entry to the dictionary
row_dict['timestamp'] = params[0]
# write row to file
writer.writerow(row_dict)
if __name__ == '__main__':
input_filenames = [r'C:\Users\cruse\Desktop\dummy_data.txt']
txt_to_csv(input_filenames)
我得到的输出是
Age PaCO2 PaO2 RecordID SysABP Temp timestamp
73 5 0:00
3400:42:00 34401:11 0:42
10501:11 35.201:11 1:11
这对这个数据集是正确的。然后,您将使用 Pandas 之类的工具通过时间传播价值。 (即使用 pd.fillna 将相同的 RecordID 分配给所有后续行)
如果您希望它处理更多文件,只需在底部的 input_filenames 列表中添加更多路径即可。