“Simpan daftar untuk mengajukan python” Kode Jawaban

Konversi daftar python ke file teks

# define list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']

with open('listfile.txt', 'w') as filehandle:
    for listitem in places:
        filehandle.write('%s\n' % listitem)
Determined Dunlin

cara menyimpan daftar python untuk mengajukan

import json
a = [1,2,3]
with open('test.txt', 'w') as f:
    f.write(json.dumps(a))

#Now read the file back into a Python list object
with open('test.txt', 'r') as f:
    a = json.loads(f.read())
Weary Wombat

Simpan daftar untuk mengajukan python

with open('your_file.txt', 'w') as f:
    for item in my_list:
        f.write("%s\n" % item)
Expensive Eel

Python Tulis daftar ke baris file demi baris

# 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)) 
Charming Caribou

cara menulis daftar ke file teks python

# define list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']

with open('listfile.txt', 'w') as filehandle:
    for listitem in places:
        filehandle.write('%s\n' % listitem)
Annoying Albatross

Jawaban yang mirip dengan “Simpan daftar untuk mengajukan python”

Pertanyaan yang mirip dengan “Simpan daftar untuk mengajukan python”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya