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

发布于 2024-12-13 05:33:50 字数 1441 浏览 0 评论 0原文

每当我尝试运行此代码时:

    #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-12-20 05:33:50

我不确定您的代码,但错误消息表明您的文件中有一个空行,并且您正在尝试将其转换为 int。例如,许多文本文件末尾都有一个空行。

我建议在转换之前先检查您的线路:

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

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:

line = ...
line = line.strip() # strip whitespace
if line: # only go on if the line was not blank
    line_value = int(line)
扮仙女 2024-12-20 05:33:50

你正在读一个空行,而 python 不喜欢这样。您可能应该检查空白行。

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

You're reading a blank line, and python doesn't like that. You should probably be checking for blank lines.

line_value = f.readline(x).strip()
if len(line_value) > 0:
    line_value = int(line_value)
    ...
心碎无痕… 2024-12-20 05:33:50

变量 a、length 和 epsilon 存在范围问题。您在 findstart 中定义它,但尝试在 findend 中访问它。

另外,传递给 readline 的变量 x 并没有按照您的想法进行。 Readline 总是返回文件中的下一行,传递给它的变量是该行长度的可选提示,而不是应该读取哪一行。要对特定行进行操作,请首先将整个文件读入列表:

# 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)

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:

# 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文