私は次のように書いています。list
は、新しい行から毎回ファイルに書きます。Python:リストをファイルごとに書き出します。
bill_List = [total_price, type_of_menu, type_of_service, amount_of_customers, discount]
このコードを使用しようとしましたが、テキストファイルが上書きされました。誰かが私を助けることができますか?私のミスはどこですか?
# attempt #1
f = open("Bills.txt", "w")
f.write("\n".join(map(lambda x: str(x), bill_List)))
f.close()
# attempt #2
# Open a file in write mode
f = open('Bills.txt', 'w')
for item in bill_List:
f.write("%s\n" % item)
# Close opend file
f.close()
# attempt #3
with 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 #4
with open('Bills.txt', 'w') as out_file:
out_file.write('\n'.join(
bill_List))
は、なぜあなたは文字列としてあなたのコードの書式を設定していますか? –
これは既にここで答えられています:http://stackoverflow.com/questions/899103/writing-a-list-to-a-file-with-python – famargar
@famargar、私もこれを試しましたが、 thefile.write( "%s \ n"%item) "ファイルを上書きするか、何かが間違っている( –