在 Python 中从 CSV 文件中提取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1526607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting data from a CSV file in Python
提问by wildfire
I just got my data and it is given to me as a csv
file.
我刚拿到我的数据,它作为一个csv
文件提供给我。
It looks like this in data studio(where the file was taken).
它在数据工作室中看起来像这样(获取文件的地方)。
Counts frequency
300 1
302 5
303 7
Excel can't handle the computations so that's why I'm trying to load it in python(it has scipy
:D).
Excel 无法处理计算,所以这就是我尝试在 python 中加载它的原因(它有scipy
:D)。
I want to load the data in an array:
我想将数据加载到数组中:
Counts = [300, 302, 303]
frequency = [1, 5, 7]
How will I code this?
我将如何编码?
回答by nosklo
import csv
counts = []
frequencies = []
for d in csv.DictReader(open('yourfile.csv'), delimiter='\t'):
counts.append(int(d['Counts']))
frequencies.append(int(d['frequency']))
print 'Counts = ', counts
print 'frequency = ', frequencies