python - Read csv while grouping rows -
i have csv file exported external application such
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
does of python csv readers handle kind of situation given output looking 3 arrays each column corresponding point headers x; y; depth; normalized field; current field; ratio.
i tried implement using regex parsing didn't work. can share ideas ?
gt
since reader classes in csv
module initialised string-iterables, suffice chunk out appropriate blocks of lines:
import csv f = open('data.txt', 'r') lines = [l l in f if len(l.split(';')) == 6] # filter relevant lines bounds = [i 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] start, end in bounds] csv_readers = [csv.dictreader(block, delimiter=';', skipinitialspace=true) block in line_blocks]
now each reader iterable on dict
instances headers keys.
r = csv_readers[0] 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'}
Comments
Post a Comment