簡體   English   中英

從.txt文件中讀取文件並從中創建條件

[英]Reading from a .txt file and making conditions from it

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)

如果您只想打印包含客戶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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM