How to access lists saved as files with Python? -


i doing natural language processing python (2.7.9) , nltk (3.2.1). way doing things, every time run program part-of-speech tagging on large corpus.

the resulting tagged corpus looks larger version of this:

[('a', 'dt'), ('better', 'jjr'), ('widower', 'jjr'), ('than', 'in'), ('my', 'prp$'), ('father', 'nn'), ('.', '.'), ('aunt', 'nnp'), ('sybil', 'nnp'), ('had', 'vbd'), ('pink-rimmed', 'jj'), ('azure', 'jj'), ('eyes', 'nns'), ('and', 'cc'), ('a', 'dt'), ('waxen', 'jj'), ('complexion', 'nn'), ('.', '.'), ('she', 'prp'), ('wrote', 'vbd'), ('poetry', 'nn'), ('.', '.'), ('she', 'prp'), ('was', 'vbd'), ('poetically', 'rb'), ('superstitious', 'jj')] 

ideally, save list file , read file variable every time run program. saving list file easy:

poscorpus = pos_tag(words)  #i convert string can write file.  poscorpus_string = str(poscorpus)  #i write file.  f = open('c:\desktop\poscorpus.txt', 'w')  f.write(poscorpus_string)  f.close() 

the problem when go read file variable, read() function reads file string—not list.

my question simple: how can read file list rather string? imagine relatively simple, not find information how it.

(apologies if off-topic or dupe.)

a string can transformed list using eval() function. said, not efficient , memory-friendly solution problem.

a better option use python's pickle or cpickle module. "pickling" refers process of saving python object (for example, list or dictionary) byte stream can unloaded variables later, without loss or deformation of object type. pickling known "serialization" , "marshalling".

here example:

#how pickle pos-tagged corpus  #pickling involves saving python object file (without first converting #it string).  #let's pickle taggedcorpus can use efficiently later:  import cpickle                                 #imports fast pickle module (written in c)  f = open('c:\desktop\taggedcorpus.p', 'w')     #creates pickle file f cpickle.dump(taggedcorpus, f)                  #dumps data of taggedcorpus object f f.close()  #to unpickle object, load file variable:  f = open('c:\desktop\taggedcorpus.p', 'r')     #opens pickle file read taggedcorpus = cpickle.load(f)                 #loads content of f taggedcorpus f.close() 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -