简体   繁体   English

如何在python中将文件行转换为float / int

[英]How to convert a file line to a float/int in python

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. 我正在扫描的文件当前只有两行,每行读为“ 3”,因此它应该输出成功,但我无法克服此错误。

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 . 我不确定您的代码,但是错误消息表明您的文件中包含空行,并且您正在尝试将其转换为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: 我建议在转换之前先检查一下您的行:

line = ...
line = line.strip() # strip whitespace
if line: # only go on if the line was not blank
    line_value = int(line)

You're reading a blank line, and python doesn't like that. 您正在读取空白行,而python不喜欢那样。 You should probably be checking for blank lines. 您可能应该检查空白行。

line_value = f.readline(x).strip()
if len(line_value) > 0:
    line_value = int(line_value)
    ...

You have a scope issue with the variables a, length and epsilon. 您在变量a,长度和epsilon方面存在范围问题。 You define it in findstart, but try to access it in findend. 您在findstart中定义它,但是尝试在findend中访问它。

Also, the variable x being passed to readline is not doing what you think. 另外,传递给readline的变量x并没有按照您的想法进行。 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. Readline始终返回文件中的下一行,传递给它的变量是行的长度的可选提示,而不是应读取的行。 To operate on specific lines, read the entire file in to a list first: 要对特定的行进行操作,请首先将整个文件读入列表中:

# Read lines from file
with open("i.txt", "r") as f:
    # Read lines and remove newline at the end of each line
    lines = [l.strip() for l in f.readlines()]

    # Remove the blank lines
    lines = filter(lambda l: l, lines)

EPSILON = 7
A = 3
length = 0

#Detect start point
def findstart( x ):
    global length

    length = 0

    line_value = int(lines[x])
    if line_value == A:
        length += 1
        x += 1
        findend(x)
    elif line_value == EPSILON:
        x += 2
        findstart(x)
    else:
        x += 1
        findstart(x)

#Detect end point
def findend(x):
    global length

    line_value = int(lines[x])
    if line_value == A:
        length += 1
        return ("Accept", length)
    elif line_value == EPSILON:
        x += 2
        length += 2
        findend(x)
    else:
        x += 1
        length += 1
        findend(x)

findstart(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM