Quantcast
Channel: Python: Write list to file, line by line - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Python: Write list to file, line by line

$
0
0

I want to write the following list to file, each time from new line.

bill_List = [total_price, type_of_menu, type_of_service, amount_of_customers, discount]

I tried to use this code but it just overwrites the text file. Could somebody help me? Where is my mistake?

# attempt #1f = open("Bills.txt", "w")f.write("\n".join(map(lambda x: str(x), bill_List)))f.close()# attempt #2# Open a file in write modef = open('Bills.txt', 'w')for item in bill_List:f.write("%s\n" % item)# Close opend filef.close()# attempt #3with open('Bills.txt', 'w') as f:for s in bill_List:    f.write(s +'\n')with open('Bills.txt', 'r') as f:bill_List = [line.rstrip('\n') for line in f]# attempt #4with open('Bills.txt', 'w') as out_file:out_file.write('\n'.join(    bill_List)) 

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images