“Salvar lista Python” Respostas de código

Como salvar a lista de Python para arquivar

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

Salvar lista para arquivar Python

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

Salvar lista Python

>>> import pickle
>>> l = [1,2,3,4]
>>> with open("test", "wb") as fp:   #Pickling
...   pickle.dump(l, fp)
... 
>>> with open("test", "rb") as fp:   # Unpickling
...   b = pickle.load(fp)
... 
>>> b
[1, 2, 3, 4]
Enthusiastic Eel

Salvar lista Python

>>> import json
>>> with open("test", "w") as fp:
...     json.dump(l, fp)
...
>>> with open("test", "r") as fp:
...     b = json.load(fp)
...
>>> b
[1, 2, 3, 4]
Enthusiastic Eel

Respostas semelhantes a “Salvar lista Python”

Perguntas semelhantes a “Salvar lista Python”

Mais respostas relacionadas para “Salvar lista Python” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código