Python 文件处理I/O读取文件
我将非常感谢您的帮助。(文本文件中的记录:巴西\n12\nethopia\n23\nmada\n10\n)。例如,当我键入“巴西”或部分开头(b或br或bra等)时,输出为: 巴西 12 我希望只有在写全名而不是开头部分时,输出才正确。我尝试将==替换为in(..,如果在第行中搜索:…),但它不起作用。所以提前谢谢你的帮助Python 文件处理I/O读取文件,python,file,search,operator-keyword,Python,File,Search,Operator Keyword,我将非常感谢您的帮助。(文本文件中的记录:巴西\n12\nethopia\n23\nmada\n10\n)。例如,当我键入“巴西”或部分开头(b或br或bra等)时,输出为: 巴西 12 我希望只有在写全名而不是开头部分时,输出才正确。我尝试将==替换为in(..,如果在第行中搜索:…),但它不起作用。所以提前谢谢你的帮助 def main(): def data(): dfile= open("coffeedata.txt","a") user = i
def main():
def data():
dfile= open("coffeedata.txt","a")
user = input('Insert a record: ')
while user == "y":
desr = input("name of coffe: ")
dfile.write(desr+"\n")
qty = float(input("insert the quantity: "))
dfile.write(str(qty)+"\n")
user = input('do you have new record: y or n: ')
else:
print("no data entry")
data()
def dreading():
with open("coffeedata.txt","r") as dfile:
for line in dfile:
print (line.rstrip("\n"))
dreading()
def searching():
found= False
search = input("name of coffee:")
with open("coffeedata.txt", "r") as dfile:
print(dfile)
for line in dfile:
if search in line:
print(line.rstrip("\n"))
print(type(line))
qty=float(dfile.readline())
print(qty)
print(type(qty))
found=True
if not found:
print("the coffee is not in the record")
searching()
main()
在您的案例中,将搜索连接到“\n”
if search + "\n" in line:
>
在您的案例中,将搜索连接到“\n”
if search + "\n" in line:
>
非常感谢,欢迎光临。您可以接受我的回答,如果您需要Hello P47 R1ck,我如何以清洁的方式修改数量。例如,将巴西之后的图12改为另一个。非常感谢您欢迎。您可以接受我的回答,如果您需要Hello P47 R1ck,我如何以清洁的方式修改数量。例如,将巴西之后的图12更改为另一个。