2016-11-17 6 views
0

は私が探しています出力であることを与えられたのpythonのcsv読者の誰もがこのような状況に対処してい 読むの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 
などの外部アプリケーションからエクスポートされたCSVファイルを持っていながら、各列がポイントヘッダXに対応する3つのアレイ。 Y;深さ;正規化されたフィールド。現在のフィールド。比。

これを正規表現解析を使用して実装しようとしましたが、動作しませんでした。誰かいくつかのアイデアを共有できますか?

GT

+0

私の解決策を試してください – eyllanesc

答えて

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'} 
+0

ありがとう@schwobaseggl:しかし、私の期待された結果は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 – user1301295

関連する問題