在对行进行分组时读取csv

时间:2016-11-17 16:14:40

标签: python csv grouping rows

我有一个从外部应用程序导出的csv文件

15/07/2008 2:48:53 PM

Measurement device:; dvp3445
Field detector:; Diode
Reference detector:; Undefined
Scan mode:; Continuous

Points [mm]:
X; Y; Depth; Normalized field; Current field; Ratio
0.0; -109.0; 20.0; 1.3; 0; 0.0
0.0; -108.7; 20.0; 1.3; 0; 0.0
0.0; -108.4; 20.0; 1.3; 0; 0.0
0.0; -108.0; 20.0; 1.3; 0; 0.0
0.0; -107.7; 20.0; 1.3; 0; 0.0


15/07/2008 5:28:50 PM

Measurement device:; dvp3445
Field detector:; Diode
Reference detector:; Undefined
Scan mode:; Continuous

Points [mm]:
X; Y; Depth; Normalized field; Current field; Ratio
0.0; -108.7; 40.0; 1.3; 0; 0.0
0.0; -108.4; 40.0; 1.4; 0; 0.0
0.0; -108.1; 40.0; 1.4; 0; 0.0
0.0; -107.8; 40.0; 1.4; 0; 0.0
0.0; -107.5; 40.0; 1.5; 0; 0.0
0.0; -107.2; 40.0; 1.6; 0; 0.0
0.0; -106.9; 40.0; 1.6; 0; 0.0


15/07/2008 5:28:50 PM

Measurement device:; dvp3445
Field detector:; Diode
Reference detector:; Undefined
Scan mode:; Continuous

Points [mm]:
X; Y; Depth; Normalized field; Current field; Ratio
0.0; -106.6; 40.0; 1.7; 0; 0.0
0.0; -106.3; 40.0; 1.8; 0; 0.0
0.0; -106.0; 40.0; 1.8; 0; 0.0
0.0; -105.7; 40.0; 1.9; 0; 0.0
0.0; -105.4; 40.0; 2.0; 0; 0.0
0.0; -105.1; 40.0; 2.1; 0; 0.0
0.0; -104.8; 40.0; 2.2; 0; 0.0
0.0; -104.5; 40.0; 2.3; 0; 0.0

是否有任何python csv阅读器处理这种情况,因为我正在寻找的输出是3个数组,每列对应于点标题X; Ÿ;深度;归一化场;当前领域;比。

我尝试使用正则表达式解析来实现它,但它不起作用。有人可以分享一些想法吗?

GT

1 个答案:

答案 0 :(得分:0)

由于csv模块中的读者类是用字符串可迭代的方式初始化的,因此只需将相应的行块分块:

import csv

f = open('data.txt', 'r')
lines = [l for l in f if len(l.split(';')) == 6]  # filter relevant lines
bounds = [i for i, line in enumerate(lines) if line[0] == 'X'] + [len(lines)]
bounds = zip(bounds, bounds[1:])  # [(0, 6), (6, 14), (14, 23)]
line_blocks = [lines[start: end] for start, end in bounds]

csv_readers = [csv.DictReader(block, delimiter=';', skipinitialspace=True) for block in line_blocks]

现在每个阅读器都是一个可迭代的dict实例,标题为键。

r = csv_readers[0]
for record in r:
    print record

{'Ratio': '0.0', 'Depth': '20.0', 'Normalized field': '1.3', 'Current field': '0', 'Y': '-109.0', 'X': '0.0'}
{'Ratio': '0.0', 'Depth': '20.0', 'Normalized field': '1.3', 'Current field': '0', 'Y': '-108.7', 'X': '0.0'}
{'Ratio': '0.0', 'Depth': '20.0', 'Normalized field': '1.3', 'Current field': '0', 'Y': '-108.4', 'X': '0.0'}
{'Ratio': '0.0', 'Depth': '20.0', 'Normalized field': '1.3', 'Current field': '0', 'Y': '-108.0', 'X': '0.0'}
{'Ratio': '0.0', 'Depth': '20.0', 'Normalized field': '1.3', 'Current field': '0', 'Y': '-107.7', 'X': '0.0'}