更改制表符分隔的csv文件的格式

时间:2012-12-09 16:22:53

标签: python csv

我从一台仪器获得了大量数据文件(.txt),表示实验数据。这是一个例子:

141716:test: 1 width: 10distance: 13 time: 1690 x:2036.1222 y:696.022 target:1925-2175
141718:test: 2 width: 10distance: 29 time: 624 x:1646.027 y:814.01953 target:1525-1775
141719:test: 3 width: 10distance: 15 time: 688 x:504.4982 y:846.8401 target:375-375
141721:test: 4 width: 10distance: 22 time: 620 x:696.42004 y:922.6398 target:550-550
141722:test: 5 width: 10distance: 10 time: 709 x:366.33945 y:950.7717 target:250-250
141724:test: 6 width: 10distance: 7 time: 602 x:2181.1575 y:641.32117 target:2075-2325
141725:test: 7 width: 10distance: 8 time: 568 x:2207.414 y:741.3456 target:2050-2300
141726:test: 8 width: 10distance: 28 time: 490 x:1629.773 y:691.3334 target:1550-1800
141727:test: 9 width: 10distance: 23 time: 479 x:1811.6924 y:651.8706 target:1675-1925
141728:test: 10 width: 10distance: 26 time: 491 x:776.4396 y:851.138 target:650-650

由于所有其他数据文件都是cvs,我已根据Convert tab-delimited txt file into a csv file using Python将这些文件转换为csv文件。我如何将上述csv文件转换为第一行是每个数据名称的格式,后续行是数据的值。我有大约一百个,所以不想手动完成。

1 个答案:

答案 0 :(得分:2)

这不是CSV。格式很糟糕。例如,widthdistance字段之间没有分隔符,有些字段在:冒号之后有空格而其他字段没有。

您必须使用自定义代码处理此问题,然后将其写入CSV文件:

import re
import csv

lineformat = re.compile(
    r'^(?P<count>\d+)[\s:]*'
    r'test[\s:]*(?P<test>\d+)[\s:]*'
    r'width[\s:]*(?P<width>\d+)[\s:]*'
    r'distance[\s:]*(?P<distance>\d+)[\s:]*'
    r'time[\s:]*(?P<time>\d+)[\s:]*'
    r'x[\s:]*(?P<x>\d+\.\d+)[\s:]*'
    r'y[\s:]*(?P<y>\d+\.\d+)[\s:]*'
    r'target[\s:]*(?P<target>\d+-\d+)[\s:]*'
)
fields = ('count', 'test', 'width', 'distance', 'time', 'x', 'y', 'target')

with open(inputfile) as finput, open(outputfile) as foutput:
    csvout = csv.DictWriter(foutput, fields=fields)
    for line in finput:
        match = lineformat.search(line)
        if match is not None:
            csvout.writerow(match.groupdict())

这使用带有命名组的正则表达式将行解析为字典,以便轻松写出CSV文件。我选择'count'作为输入文件中第一个数值的名称,随意更改它(但在正则表达式 fields元组中都这样做)