string to list when the contents is a list
Tim Chase
python.list at tim.thechases.com
Thu Feb 18 15:12:25 EST 2010
> import csv
>
> class IS_LIST():
> def __init__(self, format='', error_message='must be a list!'):
> self.format = format
> self.error_message = error_message
> def __call__(self, value):
> try:
> if value=='[]' or value=='':
> value=[]
> else:
> no_brackets = value[1:-1] # s.strip(' \t[]')
> c = csv.reader([no_brackets], quotechar="'")
> value=c.next()
> return (value, None)
> except:
> return (value, self.error_message)
> def formatter(self, value):
> return value
>
> I get an error (when I take the "try" out):
>
> AttributeError: 'function' object has no attribute 'reader'
A couple ideas occur to me:
1) you haven't copy/pasted the exact (or entirety of the) code,
and something you're doing is shadowing the "csv" module
2) are you using Python 2.x or 3.x? I don't know if the csv
module has changed in 3.x but it should work in 2.x
The first thing to check would be to pull up a raw python prompt
and see if your csv module has the expected reader:
>>> import csv
>>> csv.reader
<built-in function reader>
If not, something likely changed in 3.x and you'd have to inspect
the docs to see what happened to the reader.
If you get the above evidence of an existing reader, then you're
likely shadowing it.
-tkc
More information about the Python-list
mailing list