为什么在错误消息中发现了换行符,而实际上没有换行符?

问题描述 投票:0回答:1

此代码片段来自一个代码,旨在读取包含一百万个 pi 数字的文本文件,然后将所有六位连续数字提取到一个列表中(包括非唯一模式,以及 789012 等)。

with open('pi_million_digits.txt', 'r') as file:
    pi_file = file.read(100)

for x in range(len(pi_file)):
    if x > 1 and x < (len(pi_file) - 5):
        string_of_six = pi_file[x:x+6]    

    cons_check = []
    for digit in string_of_six:
            cons_check.append(int(digit)) 
    print(cons_check)

当我运行仅读取前 100 个字符的代码时,会出现此错误(文本文件中有不可见的

\n
,但第一个超过 100 个字符。):

    ValueError                                Traceback (most recent call last)
    Cell In[84], line 20
         18         cons_check = []
         19         for digit in string_of_six:
    ---> 20                 cons_check.append(int(digit)) 
         21         print(cons_check)
         23 #     converts string into int and puts them in a list

    ValueError: invalid literal for int() with base 10: '\n'

尝试找出导致

ValueError: invalid literal for int() with base 10: '\n'
错误的原因。

以下是我尝试过的一些事情:

with open('pi_million_digits.txt', 'r') as file:
    pi_withn = file.read(50)
    pi_file = pi_withn.replace('\n', '')
    print(file.read(50))

使用replace来尝试替换 什么都没有给出同样的错误。

print(pi_withn)
print(pi_file)

两者都返回以下内容 - 同一行上的所有字符。

3.141592653589793238462643383279502884197169399375

还打开txt文件查看。前 100 个字符中没有换行符。

python extract newline data-cleaning
1个回答
0
投票

该问题可能是由于范围可变造成的。您在第一个

string_of_six
循环中定义
for
,但在第二个
for
循环中使用它,而无需在每次迭代中重新初始化它。这可能会导致变量保留较旧的值,可能包含先前读取的换行符。

试试这个:

with open('pi_million_digits.txt', 'r') as file:
    pi_file = file.read(100)

for x in range(len(pi_file)):
    string_of_six = ""  # Initialize in each iteration
    if x > 1 and x < (len(pi_file) - 5):
        string_of_six = pi_file[x:x+6]

    cons_check = []
    for digit in string_of_six:
        cons_check.append(int(digit))
    if cons_check:
        print(cons_check)

这可确保

string_of_six
在每次迭代开始时为空,从而防止先前迭代的任何结转。

使用

pi_million_digits.txt
输出:

[8, 9, 7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