[英]What mistake am I making in my code?
For some reason my code doesn't seem to add to q, it prints out 0 for q even though there are 11 lines in the csv file, all I want to check if the csv file is empty, the code doesn't work in my script, but in the python console it works fine. 由于某些原因,我的代码似乎没有添加到q,它为q输出0,即使csv文件中有11行,我想检查csv文件是否为空,代码不起作用我的脚本,但在python控制台中它工作正常。
with open('File.csv', 'r') as FILE:
q=0;LS = reader(FILE, delimiter=',')
for i in LS:
q+=1
print q
Can anybody tell me what mistake I making? 谁能告诉我我犯了什么错误? I am really confused.
我真的很困惑。
Are you sure the file name is correct and in the same folder? 您确定文件名是否正确且位于同一文件夹中? I ran your script with a csv file I quickly made (contents: 1,2) and it outputted 1 as expected.
我用快速制作的csv文件运行你的脚本(内容:1,2)并按预期输出1。 Also make sure that your example code can be executed by itself, so include
from csv import reader
in the future. 还要确保您的示例代码可以自己执行,因此将来包含
from csv import reader
。
i
正在迭代LS
的行,所以如果你有一行1行,它将是1,如果你有一个空文件它将是0,等等...我怀疑你的文件是空的,或者开放了没有成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.