“faire du fichier de cornichon python” Réponses codées

Pickle un dictionnaire

import pickle #credits to stack overflow user= blender

a = {'hello': 'world'}

with open('filename.pkl', 'wb') as handle:
    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('filename.pkl', 'rb') as handle:
    b = pickle.load(handle)

print (a == b)
Shiny Snail

Créer un fichier Pickle Python

import pickle
file_name='my_file.pkl'
f = open(file_name,'wb')
pickle.dump(my_data,f)
f.close()
Tremendous Enceladus

fichier de cornichon ouvert Python

'''3 ways to open file with python (e.g: 'my_pickle_file.pkl)':'''

# ORIGINAL
with open('my_pickle_file.pickle', 'rb') as f :
    pickle_file = pickle.load(f)

# QUICK
pickle_file =  pickle.load(open("my_pickle_file.pickle", 'rb'))

# PANDAS
pickle_file = pd.read_pickle('my_pickle_file.pickle')
Tirbo06

faire du fichier de cornichon python

with open(MODEL_LABELS_FILENAME, "wb") as f:
    pickle.dump(lb, f)
Combative Curlew

Enregistrer un fichier en cornichon

with open('mypickle.pickle', 'wb') as f:
    pickle.dump(some_obj, f)

# note that this will overwrite any existing file
# in the current working directory called 'mypickle.pickle'
Depressed Dogfish

Comment enregistrer un fichier de cornichon

Python 3.4.1 (default, May 21 2014, 12:39:51) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['I wish to complain about this parrot what I purchased not half an hour ago from this very boutique.', "Oh yes, the, uh, the Norwegian Blue...What's,uh...What's wrong with it?", "I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!", "No, no, 'e's uh,...he's resting."]
>>> 
>>> import pickle
>>> 
>>> with open('parrot.pkl', 'wb') as f:
...   pickle.dump(mylist, f)
... 
>>>  with open('parrot.pkl', 'wb') as f:
...   new_list = pickle.load(mylist, f)

Angry Armadillo

Réponses similaires à “faire du fichier de cornichon python”

Questions similaires à “faire du fichier de cornichon python”

Plus de réponses similaires à “faire du fichier de cornichon python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code