阅读特定行不起作用[Python 3]

时间:2016-03-13 11:15:41

标签: python-3.x

我找到了一个主题,展示了如何从文本文件中打印出特定的行,但是当我在我的代码上尝试它时,它没有做任何事情,我正确地缩进了代码,但它仍然没有做任何事情。(最后一行来自SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = sdf.format(c.getTime());

if GTIN ==86947367:

如果要测试,请使用文本文件,对于需要的项目,将2和GTIN代码放入while True: itemsneeded = input("How many items do you need?") if itemsneeded.isnumeric() and int(itemsneeded) <= 5: break GTIN = '' count = 0 while count < int(itemsneeded): GTIN = (input('Please enter all GTIN-8 for all items')) if GTIN.isnumeric() and len(GTIN) == 8: Num0 = int(GTIN[0]) * 3 Num1 = int(GTIN[1]) Num2 = int(GTIN[2]) * 3 Num3 = int(GTIN[3]) Num4 = int(GTIN[4]) * 3 Num5 = int(GTIN[5]) Num6 = int(GTIN[6]) * 3 Num7 = int(GTIN[7]) total2 = (Num0 + Num1 + Num2 + Num3 + Num4 + Num5 + Num6 + Num7) if total2 % 10 == 0: print(GTIN) if GTIN in open('read_it.txt').read(): print('entered GTIN is valid') else: print('The code entered is invalid') print('Please renter this code') count += 1 else: print("The entered GTIN-8 codes are incorrect") if GTIN == 86947367: fp = open("read_it.txt") for i, line in enumerate(fp): if i == 1: print(line) elif i == 2: print(line) fp.close() 两次。 https://mega.nz/#!GQ0BhTKD!KsC4ZhFW7LJuwXXkJFyjdS9geiiJjaoUy6CAbuD6qkI

我也尝试过:

86947367

但是当我添加f = open('read_it.txt') lines = f.readlines() print(lines[1]) print(lines[2]) 时,它就停止了工作。

1 个答案:

答案 0 :(得分:1)

应该是:

if GTIN == "86947367": 

由于您从文件或输入中读取数据时,它将采用字符串格式,直到您将其重新格式化为int或其他格式。