2016-04-24 5 views
0

に都市によって並べ替えCSVへの対処私は、天気予報のデータをCSVファイルをソートしていますは、Python

New York 
"2016-04-08T07:00Z 6.2 d300 1 0.0 220 10.2 79 331" 
"2016-04-08T08:00Z 7.1 d000 1 0.0 223 10.6 74 400" 
"2016-04-08T09:00Z 7.7 d000 1 0.0 225 10.9 68 448" 
"2016-04-08T10:00Z 8.4 d000 2 0.0 225 10.9 64 553" 
"2016-04-08T11:00Z 8.9 d100 5 0.0 226 11.0 59 550" 
"2016-04-08T12:00Z 9.1 d100 8 0.0 227 11.0 57 516" 
"2016-04-08T13:00Z 8.6 d100 1 0.0 227 10.6 61 447" 
"2016-04-08T14:00Z 8.1 d100 4 0.0 227 10.1 64 362" 
Boston 
"2016-04-08T07:00Z 6.2 d300 1 0.0 220 10.2 79 331" 
"2016-04-08T08:00Z 7.1 d000 1 0.0 223 10.6 74 400" 
"2016-04-08T09:00Z 7.7 d000 1 0.0 225 10.9 68 448" 
"2016-04-08T10:00Z 8.4 d000 2 0.0 225 10.9 64 553" 
"2016-04-08T11:00Z 8.9 d100 5 0.0 226 11.0 59 550" 
"2016-04-08T12:00Z 9.1 d100 8 0.0 227 11.0 57 516" 
"2016-04-08T13:00Z 8.6 d100 1 0.0 227 10.6 61 447" 
"2016-04-08T14:00Z 8.1 d100 4 0.0 227 10.1 64 362" 

等...各都市が8つの気象データエントリを持っています。

このタイプのCSVをPythonで処理するにはどうすればよいですか?

Place、DateTime、Temperature、Attr4、Attr5などの属性を持つクラスインスタンスの配列にCSV全体を自動的にマップしたいと思いますか。この単純なコードの

with open('test.csv', 'rb') as csvfile: 
    wreader = csv.reader(csvfile, delimiter='\t', quotechar='"')  
    for row in wreader: 
     print row 

出力は、あなたが

を解析されていない二重引用符内のコンテンツをご覧のように、私はquotechar=' 'を変更して、部分的に解決される問題

['"2016-04-08T07:00Z', '6.2', 'd300', '1', '0.0', '220', '10.2', '79', '331"'] 

こと

['New York'] 
['2016-04-08T07:00Z\t6.2\td300\t1\t0.0\t220\t10.2\t79\t331'] 
['2016-04-08T08:00Z\t7.1\td000\t1\t0.0\t223\t10.6\t74\t400'] 
['2016-04-08T09:00Z\t7.7\td000\t1\t0.0\t225\t10.9\t68\t448'] 
['2016-04-08T10:00Z\t8.4\td000\t2\t0.0\t225\t10.9\t64\t553'] 
['2016-04-08T11:00Z\t8.9\td100\t5\t0.0\t226\t11.0\t59\t550'] 
['2016-04-08T12:00Z\t9.1\td100\t8\t0.0\t227\t11.0\t57\t516'] 
['2016-04-08T13:00Z\t8.6\td100\t1\t0.0\t227\t10.6\t61\t447'] 
['2016-04-08T14:00Z\t8.1\td100\t4\t0.0\t227\t10.1\t64\t362'] 

ですが、それでも二重引用符は残っていました。 削除するには?

+1

では、(簡単にGoogleができる)[CSV](https://docs.python.org/2/library/csv上に読むべき.html)モジュール! – schwobaseggl

+0

試してみましたか?あなたが達成したものを貼り付けると誰かが助けてくれるでしょう – haifzhan

答えて

1

条件付きでcsvデータを読み込み、都市名をキャプチャして項目をローカルリストに追加します。次に、クラスと辞書の定義などのその他の拡張ニーズにリストを使用します。

import csv 

weatherdata = [] 
with open('WeatherData.csv'), 'r') as csvfile: 
    readCSV = csv.reader(csvfile) 
    for line in readCSV: 
     items = [i.replace('"', '').split() for i in line][0]   
     if len(items) < 3: 
      city = items 
     else: 
      weatherdata.append([' '.join(city)] + items)  

for i in weatherdata: 
    print(i) 

# ['New York', '2016-04-08T07:00Z', '6.2', 'd300', '1', '0.0', '220', '10.2', '79', '331'] 
# ['New York', '2016-04-08T08:00Z', '7.1', 'd000', '1', '0.0', '223', '10.6', '74', '400'] 
# ['New York', '2016-04-08T09:00Z', '7.7', 'd000', '1', '0.0', '225', '10.9', '68', '448'] 
# ['New York', '2016-04-08T10:00Z', '8.4', 'd000', '2', '0.0', '225', '10.9', '64', '553'] 
# ['New York', '2016-04-08T11:00Z', '8.9', 'd100', '5', '0.0', '226', '11.0', '59', '550'] 
# ['New York', '2016-04-08T12:00Z', '9.1', 'd100', '8', '0.0', '227', '11.0', '57', '516'] 
# ['New York', '2016-04-08T13:00Z', '8.6', 'd100', '1', '0.0', '227', '10.6', '61', '447'] 
# ['New York', '2016-04-08T14:00Z', '8.1', 'd100', '4', '0.0', '227', '10.1', '64', '362'] 
# ['Boston', '2016-04-08T07:00Z', '6.2', 'd300', '1', '0.0', '220', '10.2', '79', '331'] 
# ['Boston', '2016-04-08T08:00Z', '7.1', 'd000', '1', '0.0', '223', '10.6', '74', '400'] 
# ['Boston', '2016-04-08T09:00Z', '7.7', 'd000', '1', '0.0', '225', '10.9', '68', '448'] 
# ['Boston', '2016-04-08T10:00Z', '8.4', 'd000', '2', '0.0', '225', '10.9', '64', '553'] 
# ['Boston', '2016-04-08T11:00Z', '8.9', 'd100', '5', '0.0', '226', '11.0', '59', '550'] 
# ['Boston', '2016-04-08T12:00Z', '9.1', 'd100', '8', '0.0', '227', '11.0', '57', '516'] 
# ['Boston', '2016-04-08T13:00Z', '8.6', 'd100', '1', '0.0', '227', '10.6', '61', '447'] 
# ['Boston', '2016-04-08T14:00Z', '8.1', 'd100', '4', '0.0', '227', '10.1', '64', '362'] 
+0

行[0]データは['2016-04-08T07:00Z \ t6.2 \ td300 \ t1 \ t0.0 \ t220 \ t10.2 \ t79 \ t331 ']ですline [1]が範囲外です – leonas5555

+0

リスト内包とリスト分割を使って引用構造を扱う更新されたコードを見てください(* New York *のような都市のスペースを扱う場合もあります)。 – Parfait

0

私はそれを正しく得ていれば、quotechar = ''問題を解決しました。結果は

['"2016-04-08T07:00Z', '6.2', 'd300', '1', '0.0', '220', '10.2', '79', '331"'] 
['"2016-04-08T08:00Z', '7.1', 'd000', '1', '0.0', '223', '10.6', '74', '400"'] 
['"2016-04-08T09:00Z', '7.7', 'd000', '1', '0.0', '225', '10.9', '68', '448"'] 
['"2016-04-08T10:00Z', '8.4', 'd000', '2', '0.0', '225', '10.9', '64', '553"'] 
['"2016-04-08T11:00Z', '8.9', 'd100', '5', '0.0', '226', '11.0', '59', '550"'] 
['"2016-04-08T12:00Z', '9.1', 'd100', '8', '0.0', '227', '11.0', '57', '516"'] 
['"2016-04-08T13:00Z', '8.6', 'd100', '1', '0.0', '227', '10.6', '61', '447"'] 

であり、総

weatherdata = [] 
with open('test.csv', 'r') as csvfile: 
    readCSV = csv.reader(csvfile, delimiter='\t', quotechar=' ') 
    for line in readCSV: 
     if len(line) == 1: 
      city = line[0] 
     else:    
      weatherdata.append([city] + line) 

for c in weatherdata: 
    print(c) 
+0

Nonono ....まだ二重引用符で囲まれています['"2016-04-08T07:00Z' .................. '79'、 '331' ' – leonas5555