无法从文本文件中查找和输出行 (PYTHON)
Can't Find and Output Lines From Text files (PYTHON)
我正在尝试获取用户的输入并将其与文本文件的内容进行比较。他们将输入一个八位数字,如果该数字存在于 txt 文件中,则代码应输出它所在的行。
while (len(code) == 8):
with open('GTIN Products.txt', 'r') as search:
for line in search:
line = line.rstrip('\n')
if code == line:
print(line)
当程序为运行,输入数字时,没有输出。只是空白。
两个想法:
1) 您确定打开文件和读取内容是正确的吗?
2) 您要求 8 个字符的代码与该行相同,这意味着该行也必须是 8 个字符。是这样吗?您可能想尝试
if code in line:
print(line)
break
我正在尝试获取用户的输入并将其与文本文件的内容进行比较。他们将输入一个八位数字,如果该数字存在于 txt 文件中,则代码应输出它所在的行。
while (len(code) == 8):
with open('GTIN Products.txt', 'r') as search:
for line in search:
line = line.rstrip('\n')
if code == line:
print(line)
当程序为运行,输入数字时,没有输出。只是空白。
两个想法:
1) 您确定打开文件和读取内容是正确的吗?
2) 您要求 8 个字符的代码与该行相同,这意味着该行也必须是 8 个字符。是这样吗?您可能想尝试
if code in line:
print(line)
break