2016-04-24 86 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個氣象數據條目。

如何在Python中處理這種類型的CSV?

我想自動將整個CSV映射到具有諸如Place,DateTime,Temperature,Attr4,Attr5等屬性的類實例數組......或者可能是某些其他數據結構 - 字典?這個簡單的代碼

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

輸出是

['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'] 

正如你看到在雙引號中的內容不會被解析

然後,我改變quotechar=' '和部分解決問題

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

但仍然留下雙引號。 如何移除?

+1

你應該在讀了(易谷歌能)[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

line [0]數據是['2016-04-08T07:00Z \ t6.2 \ td300 \ t1 \ t0.0 \ t220 \ t10.2 \ t79 \ t331']所以line [1]超出範圍 – leonas5555

+0

查看使用列表理解和列表拆分處理報價結構的更新代碼(甚至處理城市中的空間,如* New York *)。 – Parfait

0

所以,如果我得到了正確的報價=''解決了問題。然後結果是

['"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