简体   繁体   English

我在代码中犯了什么错误?

[英]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.

相关问题 投放时我犯了什么错误? - What mistake am I making while casting? 我在这个python脚本中犯了什么错误? - What mistake am I making in this python script? 我在哪里用这个装饰器代码犯了错误? - Where I am making a mistake with this decorator code? 我收到一个错误“代码无法访问 Pylance”这是什么意思,还是我在代码中犯了任何错误? - i am getting an error "Code is unreachable Pylance" what that mean or am i doing any mistake in my code? 我在这个简历代码中做错了什么 - What's the mistake I am doing in this CV code 我在我的猪拉丁语翻译器中犯了什么错误 - What is the mistake that i am doing in my pig latin translator Django模板无法访问字典查找变量。 我在代码中犯了什么错误? - Django template unable to access a dictionary lookup variable. What mistake am I doing in code? 我在 opencv 中使我的代码更紧凑时遇到问题 - I am having a problem with making my code more consize in opencv 我在我的代码中犯了什么错误? 它正在运行,但没有为我显示的输入提供应有的答案 - What mistake have i made in my code? Its running but not providing the answer that it should for the input i have shown 欧拉计划问题 18:我在代码中犯了什么错误? - Project Euler Problem 18: What mistake did I make in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM