简体   繁体   English

我正在读取30行的csv文件,但仅在shell中打印29行

[英]Im reading a csv file with 30 lines but only printing 29 in shell

I have a csv file im reading from and returning a column of data. 我有一个csv文件,即时通讯正在读取并返回一列数据。 There is 30 rows of data and when I print the data I'm only getting 29. 有30行数据,当我打印数据时,我只有29行。

def readMonth(fileName):
infile = open(fileName,"rb")
reader = csv.reader(infile)

month = []
for i in range(0,29):
    data = next(reader)
    month.append(int(float(data[25])))
infile.close()

return month

When I print I should have 30 lines not 29. What do I need to change in order to print all 30 lines? 打印时,我应该有30行而不是29行。要打印全部30行,我需要更改什么?

for i in range(0,29):

This starts at 0 and ends at 28. Did you mean range(30) ? 它从0开始到28结束。您是说range(30)吗? Though I'm not sure why you're not just looping over the reader . 虽然我不知道为什么你不只是遍历reader

Maybe something like this is what you're looking for? 也许您正在寻找这样的东西?

#!/usr/local/cpython-3.3/bin/python

import csv

def readMonth(fileName):
    infile = open(fileName,"r")
    reader = csv.reader(infile)

    month = []
    for row in reader:
        month.append(int(row[0]))
    infile.close()

    return month

month = readMonth('foo.csv')

print(month)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM