variable attribute name
I want to let the name of an attribute be the string value of a variable. Here is some code: class Object(object): pass A = Object() s = 'attr' A. = 1 The last line denotes the variable value by (not a python form). What I want is to have A.attr = 1, but 'attr' determined by the value of s. Please advise. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to read list from file
On Saturday, October 5, 2013 7:08:08 PM UTC-6, Harvey Greenberg wrote: > I am looping as for L in file.readlines(), where file is csv. > > > > L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first > item is a dir and 2nd is a list, so parsing with split doesn't work. Is > there a way to convert L, which is a string, to the list of 3 items I want? Yay It worked. Thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: how to read list from file
On Sunday, October 6, 2013 10:41:33 AM UTC-6, Harvey Greenberg wrote: > On Saturday, October 5, 2013 7:24:39 PM UTC-6, Tim Chase wrote: > > > On 2013-10-05 18:08, Harvey Greenberg wrote: > > > > > > > I am looping as for L in file.readlines(), where file is csv. > > > > > > > > > > > > > > L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that > > > > > > > the first item is a dir and 2nd is a list, so parsing with split > > > > > > > doesn't work. Is there a way to convert L, which is a string, to > > > > > > > the list of 3 items I want? > > > > > > > > > > > > sounds like you want ast.literal_eval(): > > > > > > > > > > > > Python 2.7.3 (default, Jan 2 2013, 13:56:14) > > > > > > [GCC 4.7.2] on linux2 > > > > > > Type "help", "copyright", "credits" or "license" for more > > > > > > information. > > > > > > >>> s = "[{'a':1, 'b':2}, [1,2,3], 10]" > > > > > > >>> import ast > > > > > > >>> print repr(ast.literal_eval(s)) > > > > > > [{'a': 1, 'b': 2}, [1, 2, 3], 10] > > > > > > > > > > > > -tkc > > > > that didn't work. printing it looks like the list because it's the input, > but try printing len(repr(ast.literal_eval(s))). It should give 3, but it > gives 72 (number of chars). None of the responses worked; after import json, I used: for line in inputFile.readlines(): L = json.loads(line.replace("","")) print L, len(L) I get error. I probably misunderstood how to implement these suggestions, but I wrote a list to a csv file whose members have lists. I now want to read them (in another program) and end up with the origianl list. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to read list from file
On Saturday, October 5, 2013 7:24:39 PM UTC-6, Tim Chase wrote: > On 2013-10-05 18:08, Harvey Greenberg wrote: > > > I am looping as for L in file.readlines(), where file is csv. > > > > > > L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that > > > the first item is a dir and 2nd is a list, so parsing with split > > > doesn't work. Is there a way to convert L, which is a string, to > > > the list of 3 items I want? > > > > sounds like you want ast.literal_eval(): > > > > Python 2.7.3 (default, Jan 2 2013, 13:56:14) > > [GCC 4.7.2] on linux2 > > Type "help", "copyright", "credits" or "license" for more > > information. > > >>> s = "[{'a':1, 'b':2}, [1,2,3], 10]" > > >>> import ast > > >>> print repr(ast.literal_eval(s)) > > [{'a': 1, 'b': 2}, [1, 2, 3], 10] > > > > -tkc that didn't work. printing it looks like the list because it's the input, but try printing len(repr(ast.literal_eval(s))). It should give 3, but it gives 72 (number of chars). -- https://mail.python.org/mailman/listinfo/python-list
how to read list from file
I am looping as for L in file.readlines(), where file is csv. L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first item is a dir and 2nd is a list, so parsing with split doesn't work. Is there a way to convert L, which is a string, to the list of 3 items I want? -- https://mail.python.org/mailman/listinfo/python-list