将第二列python csv文件作为整数数组获取

时间:2017-04-08 22:17:58

标签: python arrays csv int

我有一个csv文件,如下所示:

                #just want this row as array of ints
8/8/2016 0:00   15804300.00     
8/9/2016 0:00   15805850.00     
8/10/2016 0:00  15807737.50
...

我想把第二列数字(15804300.00 ...)作为一个整数数组得到,所以它就像:

[15804300.00, 15805850.00, 15807737.50...]

我遇到了类型转换错误,但这是我到目前为止所做的:

filename = 'data.csv' 
with open(filename, 'rb') as f:
    reader = csv.reader(f)
    dataList = list(reader)

    num = []
    for row in dataList:
        num.append(row[0].strip().split('\t'))   #split by tab 

有什么建议吗?有更简单的方法吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

如果您的数据是制表符分隔,请告诉csv.reader()对象使用该分隔符。手动拆分应该没有任何理由:

filename = 'data.csv' 
with open(filename, 'rb') as f:
    reader = csv.reader(f, delimiter='\t')
    num = [float(row[1]) for row in reader]

列表推导然后选出每行的第二列,将字符串转换为浮点值。

演示:

>>> import csv
>>> demodata = BytesIO("""\
... 8/8/2016 0:00\t15804300.00
... 8/9/2016 0:00\t15805850.00
... 8/10/2016 0:00\t15807737.50
... """)
>>> with demodata as f:
...     reader = csv.reader(f, delimiter='\t')
...     num = [float(row[1]) for row in reader]
...
>>> num
[15804300.0, 15805850.0, 15807737.5]

答案 1 :(得分:-1)

This works

import csv
filename = 'data.csv'
with open(filename, 'rb') as f:
    reader = csv.reader(f, delimiter='\t')
    dataList = list(reader)
    num = []
    for row in dataList:
        num.append(float(row[0].strip().split(' ')[-1]))   #split with spaces, and extract the last item, as it is always the last one

    print num #[15804300.0, 15805850.0, 15807737.5]