亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 txt 文件的多行與 python 中的單個變量進行比較

將 txt 文件的多行與 python 中的單個變量進行比較

largeQ 2023-05-23 16:38:29
我想知道如何將文本文件的多行與單個變量進行比較。我已經部分工作了,但它只與文本文件的最后一行進行比較def loginSetup():    global loginSelector    global accountInt    loginSelector = int(input("Select Action:"))            if loginSelector == 1:        #login        print ("action complete")    if loginSelector == 2:        #sign up          accountInt = int(input("Input 4 Digit Pin:"))        while (accountInt >= 9999 or accountInt <= 999):            print("ERROR\nTry Again")            accountInt = int(input("Input 4 Digit Pin:"))        accountInt = str(accountInt)        with open('Account.txt', 'r') as rf:            for line in rf:                if (line == str(accountInt)):                    print("error")        with open('Account.txt', 'a') as f:            f.write('\n')            f.write(accountInt)            while True:     loginSetup() 
查看完整描述

1 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

這是因為您不是先寫一行文本后跟換行符的標準方式,而是先寫換行符。所以文件的最后一行沒有尾隨換行符(并允許比較在那里成功)。

在循環中,line將是一些在末尾帶有換行符的文本(除了最后一行之外的所有文本),并且str(AccountInt)永遠不會有換行符。所以不可能匹配。

在比較之前,您需要從字符串中去除換行符。


查看完整回答
反對 回復 2023-05-23
  • 1 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號