Python - Keeping text file in one line? -
i'm making code shows product list in ordered way, , user can input gtin-8 code , select amount wish 'purchase'. when user inputs gtin-8 code, fullline should product description etc in product list , show amount. however, amount appearing on new line don't want. i've tried putting newline after product list, before , under it, won't stay on same line. here 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 purchases: \n") receipt.close() print("please enter gtin-8 codes of products want , how many") iffinished="" while iffinished != "yes": productswanted=input("please enter gtin-8 code of product: ") amountofproducts=input("how many want? ") open("productsfile.txt") f: 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, get, e.g.:
23456945, thorntons chocolate box, £10.00 amount: 2
however want amount on same line
using rstrip method, change fullline=(line +"amount: " +amountofproducts)
to fullline=(line.rstrip() +"amount: " +amountofproducts)
Comments
Post a Comment