我试图从CSV文件中获取数据,并且有三列("日期",电台"," pcp"),包括41年的数据集。我想分别得到这些数据。
示例数据集:
date stations pcp
1.01.1979 6 1.071
2.01.1979 6 5.909
3.01.1979 6 9.134
1.01.1979 5 1.229
2.01.1979 5 0.014
3.01.1979 5 3.241
当l运行代码时,我得到了这个输出
date;stations;pcp None None
2.04.1979;6;0.0 None None
3.04.1979;6;0.0 None None
4.04.1979;6;0.35 None None
5.04.1979;5;0.003 None None
日期字段包括所有数据,但是电台和pcp字段是"无" 怎么解决呢?
这是我的代码
import csv
import numpy as np
with open('p2.csv') as csvfile:
reader = csv.DictReader(csvfile,fieldnames=("date","stations","pcp"),delimiter=' ', quotechar='|')
for row in reader:
print(row["date"],row["stations"],row["pcp"])
答案 0 :(得分:1)
您的输入文件(SELECT IF(COUNT(CASE WHEN color = 'blue' THEN 1 END) > 0, 1, 0) AS color,
IF(COUNT(CASE WHEN color = 'line' THEN 1 END) > 0, 1, 0) AS form
FROM mytable
WHERE type = 42
GROUP BY type
):
p2.csv
您的代码:
date stations pcp
1.01.1979 6 1.071
2.01.1979 6 5.909
3.01.1979 6 9.134
1.01.1979 5 1.229
2.01.1979 5 0.014
3.01.1979 5 3.241
输出:
import csv
import numpy as np
with open('p2.csv') as csvfile:
reader = csv.DictReader(csvfile,fieldnames=("date","stations","pcp"),delimiter=' ', quotechar='|')
for row in reader:
print(row["date"],row["stations"],row["pcp"])
输入文件存在严重问题。分隔符不是单个空格,它有多个空格。