[英]Python - Keeping text file in one line?
I'm making a code that shows a product list in an ordered way, and the user can input a GTIN-8 code and select an amount they wish to 'purchase'. 我正在编写一个以有序方式显示产品列表的代码,用户可以输入GTIN-8代码并选择他们希望“购买”的数量。 So when the user inputs a GTIN-8 code, FullLine should be the product description etc which is in the product list and show the amount.
因此,当用户输入GTIN-8代码时,FullLine应该是产品说明等,该说明位于产品列表中并显示金额。 However, the amount is appearing on a new line which I don't want.
但是,该金额显示在我不希望的新行上。 I've tried putting the newline after the product list, before it and under it, but it won't stay on the same line.
我尝试将换行符放在产品列表之后,之前和之下,但不会停留在同一行上。 Here is the code:
这是代码:
nl="\n"
Products=open("Productsfile.txt","w")
Products.write(nl+"23456945, Thorntons Chocolate Box, £10.00")
Products.write(nl+"12376988, Cadburys Easter Egg, £15.00")
Products.write(nl+"76543111, Galaxy Bar, £1.00")
Products.write(nl+"92674769, Cadury Oreo Bar, £1.00")
Products.write(nl+"43125999, Thorntons Continental Box, £12.00")
Products.close()
Products=open("Productsfile.txt","r")
print(Products.read())
Receipt=open("ReceiptFile.txt","w")
Receipt.write("Here are your purchases: \n")
Receipt.close()
print("Please enter the GTIN-8 Codes of the products you want and how many")
IfFinished=""
while IfFinished != "Yes":
ProductsWanted=input("Please enter the GTIN-8 Code of the product: ")
AmountOfProducts=input("How many do you want? ")
with open("Productsfile.txt") as f:
for line in f:
if ProductsWanted in line:
FullLine=(line +"Amount: " +AmountOfProducts)
print(FullLine)
Receipt=open("ReceiptFile.txt","a")
Receipt.write(str(FullLine))
Receipt.close()
So when running, I get, eg: 因此,在运行时,我得到了例如:
23456945, Thorntons Chocolate Box, £10.00
Amount: 2
However I want the Amount on the same line 但是我希望金额在同一行
Using rstrip method, Change the FullLine=(line +"Amount: " +AmountOfProducts)
使用rstrip方法,更改
FullLine=(line +"Amount: " +AmountOfProducts)
to FullLine=(line.rstrip() +"Amount: " +AmountOfProducts)
到
FullLine=(line.rstrip() +"Amount: " +AmountOfProducts)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.