我是Python新手。我正在尝试将数据从文本文件传输到csv文件。我已经包含了我的文本文件和csv文件的简短描述。有人能指出我正确的方向阅读以完成这项工作吗?
**Input Text file**
01/20/18 12:19:35#
TARGET_CENTER_COLUMN=0
TARGET_CENTER_ROW=0
TARGET_COLUMN=0
BASELINE_AVERAGE=0
#
01/21/18 12:19:35#
TARGET_CENTER_COLUMN=0
TARGET_CENTER_ROW=13
TARGET_COLUMN=13
BASELINE_AVERAGE=26
#
01/23/18 12:19:36#
TARGET_COLUMN=340
TARGET_CENTER_COLUMN=223
TARGET_CENTER_ROW=3608, 3609, 3610
BASELINE_AVERAGE=28
#
01/24/18 12:19:37#
TARGET_CENTER_COLUMN=224
TARGET_CENTER_ROW=388
TARGET_COLUMN=348
BASELINE_AVERAGE=26
#
01/25/18 12:19:37#
TARGET_CENTER_COLUMN=224
TARGET_CENTER_ROW=388
TARGET_COLUMN=348
BASELINE_AVERAGE=26
#
01/27/18 12:19:37#
TARGET_CENTER_COLUMN=223
TARGET_COLUMN=3444
TARGET_CENTER_ROW=354
BASELINE_AVERAGE=25
#
**Output CSV file**
Date,Time,BASELINE_AVERAGE,TARGET_CENTER_COLUMN,TARGET_CENTER_ROW,TARGET_COLUMN
01/20/18,9:37:16 PM,0,0,0,0
01/21/18,9:37:16 PM,26,0,13,13
01/23/18,9:37:16 PM,28,223,3608,340
0,0,3609,0
0,0,3610,0
01/24/18,9:37:16 PM,26,224,388,348
01/25/18,9:37:16 PM,26,224,388,348
01/27/18,9:37:16 PM,25,223,354,344
在线阅读我已经能够实现这一点。
import csv
txt_file = r"DebugLog15test.txt"
csv_file = r"15test.csv"
mylist = ['Date','Time','BASELINE_AVERAGE' ,'TARGET_CENTER_COLUMN', 'TARGET_CENTER_ROW','TARGET_COLUMN']
in_txt = csv.reader(open(txt_file, "r"))
with open(csv_file, 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
除此之外,我计划启动一个for循环并读取数据直到#,因为这将是1行,然后使用分隔符找到每个'='并将数据插入行列表中的适当位置(do这通过比较列标题和分隔符之前的字符串并相应地填充行。你认为这种方法是否正确?
感谢您的帮助!
答案 0 :(得分:0)
查看csv.DictWriter以获得更好的方法。你给它一个标题列表,然后你可以给它为你写的数据字典。写作部分看起来像这样:
import csv
csv_file = "15test.csv"
headers = ['Date','Time','BASELINE_AVERAGE' ,'TARGET_CENTER_COLUMN', 'TARGET_CENTER_ROW','TARGET_COLUMN']
with open(csv_file, 'w') as myfile:
wr = csv.DictWriter(myfile, quoting=csv.QUOTE_ALL, headers = headers)
# data_dicts is a list of dictionaries looking like so:
# {'Date': '01/20/18', 'Time': '12:19:35', 'TARGET_CENTER_COLUMN': '0', ...}
wr.writerows(data_dicts)
至于阅读您的输入,csv.reader
将没有多大帮助:您的输入文件实际上不像csv文件。你可能最好自己编写自己的解析,虽然由于输入格式不一致会有点麻烦。这是我如何接近这一点。首先,创建一个函数来解释每一行:
def get_data_from_line(line):
line = line.strip()
if line == '#':
# We're between data sections; None will signal that
return None
if '=' in line:
# this is a "KEY=VALUE" line
key, value = line.split('=', 1)
return {key: value}
if ' ' in line:
# this is a "Date time" line
date, time = line.split(' ', 1)
return {'Date': date, 'Time': time}
# if we get here, either we've missed something or there's bad data
raise ValueError("Couldn't parse line: {}".format(line))
然后从输入文件构建数据字典列表:
data_dicts = []
with open(txt_file) as infh:
data_dict = {}
for line in infh:
update = get_data_from_line(line)
if update is None:
# we're between sections; add our current data to the list,
# if we have data.
if data_dict:
data_dicts.append(data_dict)
data_dict = {}
else:
# this line had some data; this incorporates it into data_dict
data_dict.update(update)
# finally, if we don't have a section marker at the end,
# we need to append the last section's data
if data_dict:
data_dicts.append(data_dict)