Python ValueError:long() 的无效文字,基数为 10:''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15511670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: invalid literal for long() with base 10: ''
提问by cerber
How do I get this to work?
我如何让这个工作?
n = 1234
f = open("file", "r")
while True:
x=f.readline()
print "*********************"
print n%(long(x))
if n%(long(x))==0:
print x
else:
print "..."
I'm a noob in python and I'm getting an error I don't understand. What am I doing wrong?
我是 python 的菜鸟,遇到了一个我不明白的错误。我究竟做错了什么?
ValueError: invalid literal for long() with base 10: ''
回答by unutbu
In [104]: long('')
ValueError: invalid literal for long() with base 10: ''
This error is telling you that xis the empty string.
此错误告诉您这x是空字符串。
You could be getting this at the end of the file. It can be fixed with:
你可能会在文件的末尾得到这个。它可以通过以下方式修复:
while True:
x = f.readline()
if x == '': break
回答by John La Rooy
A try/exceptblock can be a handy way to debug things like this
一个try/except块可以调试这样的事情的简便方法
n = 1234
f = open("file", "r")
while True:
x=f.readline()
print "*********************"
try: # Add these 3 lines
print n%(long(x))
except ValueError: # to help work out
print "Something went wrong {!r}".format(x) # the problem value
if n%(long(x))==0:
print x
else:
print "..."

