如何在Python中将文件行转换为float/int
每当我尝试运行此代码时:
#Open file
f = open("i.txt", "r")
line = 1
#Detect start point
def findstart( x ):
length = 0
epsilon = 7
a = 3
line_value = int(f.readline(x))
if line_value == a:
length = length + 1
x = x + 1
findend(x)
elif line_value == epsilon:
x = x + 2
findstart(x)
else:
x = x + 1
findstart(x)
#Detect end point
def findend(x):
line_value = int(f.readline(x))
if line_value == a:
length = length + 1
return ("Accept", length)
elif line_value == epsilon:
x = x + 2
length = length + 2
findend(x)
else:
x = x + 1
length = length + 1
findend(x)
findstart(line)
我收到此错误代码:
Traceback (most recent call last):
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 39, in <module>
findstart(line)
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 16, in findstart
findend(x)
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 26, in findend
line_value = int(f.readline(x))
ValueError: invalid literal for int() with base 10: ''
任何人都可以帮助我找出问题所在吗?在我看来,它正在尝试读取一个空单元格,但我不知道为什么会这样。我正在扫描的文件当前只有两行,每行读取“3”,因此它应该输出成功,但我无法克服此错误。
Whenever I try to run this code:
#Open file
f = open("i.txt", "r")
line = 1
#Detect start point
def findstart( x ):
length = 0
epsilon = 7
a = 3
line_value = int(f.readline(x))
if line_value == a:
length = length + 1
x = x + 1
findend(x)
elif line_value == epsilon:
x = x + 2
findstart(x)
else:
x = x + 1
findstart(x)
#Detect end point
def findend(x):
line_value = int(f.readline(x))
if line_value == a:
length = length + 1
return ("Accept", length)
elif line_value == epsilon:
x = x + 2
length = length + 2
findend(x)
else:
x = x + 1
length = length + 1
findend(x)
findstart(line)
I get this error code:
Traceback (most recent call last):
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 39, in <module>
findstart(line)
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 16, in findstart
findend(x)
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 26, in findend
line_value = int(f.readline(x))
ValueError: invalid literal for int() with base 10: ''
Can anyone help me to figure out what's wrong? It seems to me that it's attempting to read an empty cell but I don't know why that would be. The file I'm scanning currently only has two lines with each reading "3" so it should output a success but I can't get past this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定您的代码,但错误消息表明您的文件中有一个空行,并且您正在尝试将其转换为
int
。例如,许多文本文件末尾都有一个空行。我建议在转换之前先检查您的线路:
I'm not sure about your code, but the error message suggests that your file has an empty line in it and you're trying to convert it to an
int
. For example, a lot of text files have an empty line at the end.I'd suggest first checking your line before converting it:
你正在读一个空行,而 python 不喜欢这样。您可能应该检查空白行。
You're reading a blank line, and python doesn't like that. You should probably be checking for blank lines.
变量 a、length 和 epsilon 存在范围问题。您在 findstart 中定义它,但尝试在 findend 中访问它。
另外,传递给 readline 的变量 x 并没有按照您的想法进行。 Readline 总是返回文件中的下一行,传递给它的变量是该行长度的可选提示,而不是应该读取哪一行。要对特定行进行操作,请首先将整个文件读入列表:
You have a scope issue with the variables a, length and epsilon. You define it in findstart, but try to access it in findend.
Also, the variable x being passed to readline is not doing what you think. Readline always returns the next line in the file, the variable passed to it is an optional hint of what the length of the line might be, it is not which line should be read. To operate on specific lines, read the entire file in to a list first: