在Python中处理按城市排序的csv

2024-10-04 01:36:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我已将CSV文件与天气预报数据进行了排序:

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映射到类实例数组的属性,如地点,日期时间,温度,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"'] 

但还是留下了双引号。 如何删除?你知道吗


Tags: csv数据csvfilenewt1yorkquotechard100
2条回答

所以如果我做对了,问题就解决了。结果就是

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

有条件地读取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']

相关问题 更多 >