如何修复文本文件的解析?

How to fix parsing of textfile?

我正在尝试解析一个文本文件,然后我可以决定它是在 y=x 行之上、之上、之上还是之下。代码运行通过,但没有执行我的 readline()s.

我正在使用的文本文件是这样的:

4
2
1
3
3
5
6
7
8

我通过 readline() 的不同形式更改了代码。

if res == "yes":
    with open('text.txt', 'r') as f:
        alist = [line.rstrip() for line in f]
        timesgo = 0
        terms = f.readline()
        while str(terms) > str(timesgo):
            temp = f.readline()
            y = f.readline()
            if temp < y:
                u = str(temp) + "," + str(y)
                Above.append(u)
                timesgo += 1
            elif temp == y:
                u = str(temp) + "," + str(y)
                On.append(u)
                timesgo += 1
            if temp > y:
                u = str(temp) + "," + str(y)
                Below.append(u)
                timesgo += 1

我希望总共有 4 个坐标,在文件的情况下:(2,1)(3,3)(5,6)(7,8) 应该附加到适当的列表中然后按进一步的代码分类。

我的版本:

if res == "yes":
    with open('text.txt') as f:
        terms = f.readline()
        for _ in range(int(terms)):

            x = int(f.readline())
            y = int(f.readline())

            #u = (x, y)              # as tuple with numbers
            u = "{},{}".format(x, y) # as string

            if x < y:
                Above.append(u)
            elif x > y:
                Below.append(u)
            else
                On.append(u)

你的错误是

alist = [line.rstrip() for line in f]

它正在读取文件中的所有行,后来 readline() 没有任何内容可读。

其他变化只是装饰性的。