2

I searched for a way to save variables into a file (making them persistent for other computations).

I found some solutions like: https://stackoverflow.com/a/899199/1846113 but when I implemented it on a list like:

import pickle

list = [['cccc',['asd','sdad','sdadas']],['cscc',['asd','sdad','sdadas']]]
pickle.dump(list, outfile)

It gives me this error

 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1370, in dump
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 600, in save_list
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 615, in _batch_appends
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 600, in save_list
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 615, in _batch_appends
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 739, in save_global
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 811, in whichmodule
TypeError: unhashable type: 'list'

Anyone know what the problem is? Or other solutions?


Edit: with solution

The problem was that I made an error creating the list. I'll post it (so you can laugh) and avoid this stupid error: I was creating the list by processing some elements of a list with an (ugly) function:

def process_element(doc):
    processed_value = do_something(doc.pop())
    return [doc.pop, processed_value] 

As some of you already notice, I've made an error returning the output:

[doc.pop, processed_value]

I added a method, which is not hashable, to the list, giving me the error. The correct version is:

def process_element(doc):
    processed_value = do_something(doc.pop())
    return [doc.pop(), processed_value] 

Thanks.

Community
  • 1
  • 1
dbonadiman
  • 267
  • 2
  • 7
  • 4
    Works perfectly fine for me (but please "fix" #the really ugly shadowing of the builtin `list` by calling your variable `list`, too) – ThiefMaster Nov 27 '12 at 21:37
  • i don't know. my code to obtain this list is too long for post it here but after some operation i'm in the situation above, and its gives me an error, but i'm sure that my list looks like this – dbonadiman Nov 27 '12 at 21:49
  • 1
    @Dproof http://sscce.org/ What you posted works fine. – MK. Nov 27 '12 at 21:53
  • i've post a solution for the problem. as you mentioned the problem was not here but above. – dbonadiman Nov 27 '12 at 22:09

2 Answers2

0

A simpler way is to use with, for and enumerate like this :

with open('input.txt', 'r') as f, open('output.txt', 'w') as o:
    for numline, line in enumerate((line.split() for line in f), start=1):
        # process line elements by using line[0] line[1]...

    # When you're done, you can write results in an output file like this
    # (add a loop if needed):
    o.write("%s %s\n" % (results[0], results[1]))

Et voilà !

Jérôme Radix
  • 9,159
  • 3
  • 29
  • 38
0

For your example list, and most others, I would use JSON file format:

import json

lst = [['cccc',['asd','sdad','sdadas']],['cscc',['asd','sdad','sdadas']]]
with open('list.json', w) as jsonout:
    json.dump(lst, jsonout)

# and then
with (open('list.json') as jsonin:
    lst = json.load(jsonin)
heltonbiker
  • 23,225
  • 20
  • 121
  • 212