简体   繁体   English

用python读取csv文件

[英]Read out of a csv file with python

I want to read the last 24 rows of a csv file using python. 我想使用python读取csv文件的最后24行。 So for example if the csv Data has 500 rows, I want to write row 476 to row 500. I dont know how many rows the csv file has. 因此,例如,如果csv数据有500行,我想将476行写入500行。我不知道csv文件有多少行。 I tried this code, doesn't work. 我尝试了此代码,无法正常工作。 Can someone help me? 有人能帮我吗?

csvData.readlines()[-24:]
    html.write('{ x: new Date('+ row[0] + '), y:' + row[2] + '},')
csvData.readlines()[-24:]

returns a list of all lines in your file. 返回文件中所有行的列表。 Appropriate code could be: 适当的代码可以是:

for row in csvData.readlines()[-24:]:
    html.write('{ x: new Date('+ row[0] + '), y:' + row[2] + '},')

You need to use a for loop to read the lines. 您需要使用for循环来读取行。 csvData.readlines() returns a list of lines. csvData.readlines()返回行列表。 So, by doing something like below you can achieve what you want: 因此,通过执行以下类似操作,您可以实现所需的功能:

for row in csvData.readlines()[-n:]: # you can read the last n lines

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

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