3 回答

TA貢獻1752條經(jīng)驗 獲得超4個贊
讓我們分解一下您當(dāng)前的陳述:
if "docket = 3ghi" == True:
非空字符串的計算結(jié)果類似于True,但不完全是 True。True是一個布爾值,所以你問“這個字符串是一個布爾值嗎?” 那總是False:
"somestr" == True
# False
修復(fù)它以檢查字符串是否是in文件的一部分。例如:
with open(("test.txt",'r') as new:
for line in new.read(): # read in the file and iterate
if "somestr" in line:
# do something
注意我還添加了括號,new.read()這樣你就不會得到像function doesn't support iteration

TA貢獻1794條經(jīng)驗 獲得超8個贊
第一個問題:
readin = new.read
您沒有調(diào)用該方法,您將無法獲取文件內(nèi)容 readin
第二個問題:
if "docket =3ghi" == True:
您正在比較字符串是否為True
- 它從不True
。

TA貢獻1828條經(jīng)驗 獲得超6個贊
您需要正確調(diào)用該方法才能將文件的全部內(nèi)容放在readin. 現(xiàn)在你可以replace兩次檢查也正確的條件
with open('test.txt', 'r') as new:
readin = new.read()
if 'docket="3ghi"' in readin:
readin = readin.replace('"I"', '"new"').replace('"II"', '"fair"')
# save or print or do whatever you want with readin
添加回答
舉報