问题阅读csv文件(Issue reading csv file)

编程入门 行业动态 更新时间:2024-10-28 20:18:58
问题阅读csv文件(Issue reading csv file)

我在使用python读取已保存的csv时遇到问题:

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = sum(1 for row in data) print row_count r = 1 for row in data: print r

我的问题是虽然python似乎识别文件并识别出row_count = 9(并打印出来),但它不会为后一循环中的每一行打印r。

此代码在我的计算机上正常运行,但不在www.pythonanywhere.com上的云中运行

I am having trouble reading a saved csv with python:

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = sum(1 for row in data) print row_count r = 1 for row in data: print r

My issue is that although python seems to recognise the file and recognise that row_count = 9 (and prints this) it doesn't print r for each row in the later loop.

This code runs correctly on my computer, but not in the cloud on www.pythonanywhere.com

最满意答案

这是因为在以下行中 - row_count = sum(1 for row in data) - 您已经读完了文件并且它已经到了结尾。 所以,当你再次尝试做 -

for row in data: print r

它不起作用,因为data文件在最后。

您可以尝试的许多事情之一是再次重新打开文件以从头开始读取它。

示例 -

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = sum(1 for row in data) print row_count with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) r = 1 for row in data: print r

虽然您也可以对行进行计数并将行打印成单个循环,如 -

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = 0 for row in data row_count += 1 print row print row_count

你可以做的另一件事是 -

csvfile.seek(0) #to make the file point to the start.

示例 -

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = sum(1 for row in data) print row_count csvfile.seek(0) r = 1 for row in data: print r

That is because in the following line - row_count = sum(1 for row in data) - you have already read through the file and it has reached its end. So when you again try to do -

for row in data: print r

It would not work, because data file is at the end.

One of the many things you can try is re-openning the file again to read it from start.

Example -

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = sum(1 for row in data) print row_count with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) r = 1 for row in data: print r

Though you can also make both counting of lines and printing the line into a single loop like -

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = 0 for row in data row_count += 1 print row print row_count

Another thing you can do is -

csvfile.seek(0) #to make the file point to the start.

Example -

import csv with open('blah.csv','rb') as csvfile: data = csv.reader( csvfile ) row_count = sum(1 for row in data) print row_count csvfile.seek(0) r = 1 for row in data: print r

更多推荐

本文发布于:2023-07-21 13:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1210742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   csv   Issue   file   reading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!