client = input("What is your Client ID. CaSe SeNsItIvE")
file = open("clientIntensity.txt" , "r")
found = False
for line in file:
if (client) in line:
found = True
我想制作程序,以便在输入正确的客户端ID时,它会在给定的.txt文件中搜索ID。找到后,它将打印6-14个字符(它们是“高”或“中等”)。然后它将根据6-14个字符打印一些内容。到目前为止,我做了那么多......谢谢! (Python 3.4.2)
答案 0 :(得分:0)
如果您只想打印包含客户端ID的行的第6到第14个字符,可以执行以下操作:
client = input("What is your Client ID. CaSe SeNsItIvE")
file = open("clientIntensity.txt" , "r")
for line in file:
if client in line:
print(line[5:13])
break
(你使用5和13来获得第6-14个字符,因为第一个字符编号为零)
编辑。如果你想根据第6-14个字符的内容打印一些东西,你可以这样做:
def get_security(filename, client_id):
with open(filename, 'r') as f:
for line in f:
if client_id in line:
return line[5:13]
client = input("What is your Client ID. CaSe SeNsItIvE")
security = get_security('clientIntensity.txt', client)
if security is None:
print ("Client ID not found")
elif security.startswith('High'):
print('Good')
elif security == 'Moderate':
print('Bad')
else:
print('There has been an error')