Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56228 > unrolled thread

how to read list from file

Started byHarvey Greenberg <hjgreenberg@gmail.com>
First post2013-10-05 18:08 -0700
Last post2013-10-06 23:08 +0530
Articles 12 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-05 18:08 -0700
    Re: how to read list from file Tim Chase <python.list@tim.thechases.com> - 2013-10-05 20:24 -0500
      Re: how to read list from file Roy Smith <roy@panix.com> - 2013-10-05 21:35 -0400
      Re: how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-06 09:41 -0700
        Re: how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-06 09:46 -0700
        Re: how to read list from file Ravi Sahni <ganeshsahni07@gmail.com> - 2013-10-06 22:20 +0530
        Re: how to read list from file Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-07 09:04 +0300
    Re: how to read list from file Roy Smith <roy@panix.com> - 2013-10-05 21:34 -0400
    Re: how to read list from file Terry Reedy <tjreedy@udel.edu> - 2013-10-06 02:24 -0400
    Re: how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-06 09:57 -0700
      Re: how to read list from file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-06 18:24 +0100
      Re: how to read list from file Ravi Sahni <ganeshsahni07@gmail.com> - 2013-10-06 23:08 +0530

#56228 — how to read list from file

FromHarvey Greenberg <hjgreenberg@gmail.com>
Date2013-10-05 18:08 -0700
Subjecthow to read list from file
Message-ID<fbc6e512-88fa-4de0-80d3-6757fcc52af4@googlegroups.com>
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?

[toc] | [next] | [standalone]


#56230

FromTim Chase <python.list@tim.thechases.com>
Date2013-10-05 20:24 -0500
Message-ID<mailman.767.1381022585.18130.python-list@python.org>
In reply to#56228
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







[toc] | [prev] | [next] | [standalone]


#56232

FromRoy Smith <roy@panix.com>
Date2013-10-05 21:35 -0400
Message-ID<roy-9023B2.21353105102013@news.panix.com>
In reply to#56230
In article <mailman.767.1381022585.18130.python-list@python.org>,
 Tim Chase <python.list@tim.thechases.com> wrote:

> sounds like you want ast.literal_eval():

This sounds like a better idea than either of my earlier suggestions!

[toc] | [prev] | [next] | [standalone]


#56268

FromHarvey Greenberg <hjgreenberg@gmail.com>
Date2013-10-06 09:41 -0700
Message-ID<61d4be5e-5483-4e1c-9b85-8dac7d6d5ea2@googlegroups.com>
In reply to#56230
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).

[toc] | [prev] | [next] | [standalone]


#56269

FromHarvey Greenberg <hjgreenberg@gmail.com>
Date2013-10-06 09:46 -0700
Message-ID<6b202854-c905-48f3-a335-8fde2a8e8158@googlegroups.com>
In reply to#56268
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.

[toc] | [prev] | [next] | [standalone]


#56271

FromRavi Sahni <ganeshsahni07@gmail.com>
Date2013-10-06 22:20 +0530
Message-ID<mailman.788.1381078230.18130.python-list@python.org>
In reply to#56268
On Sun, Oct 6, 2013 at 10:11 PM, Harvey Greenberg <hjgreenberg@gmail.com> wrote:
> On Saturday, October 5, 2013 7:24:39 PM UTC-6, Tim Chase wrote:
>>   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).

Please to remove the repr and try again?
Thank you!

-- 
Ravi

[toc] | [prev] | [next] | [standalone]


#56304

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-10-07 09:04 +0300
Message-ID<qotli2562hr.fsf@ruuvi.it.helsinki.fi>
In reply to#56268
Harvey Greenberg writes:

> On Saturday, October 5, 2013 7:24:39 PM UTC-6, Tim Chase wrote:
> >   >>> 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]
>
> 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).

Not sure what the "print repr( )" is meant to achieve here, but I
think you should be able to see through it:

  >>> ast.literal_eval(s)
  [{'a': 1, 'b': 2}, [1, 2, 3], 10]
  >>> len(ast.literal_eval(s))
  3
  >>> 

[toc] | [prev] | [next] | [standalone]


#56231

FromRoy Smith <roy@panix.com>
Date2013-10-05 21:34 -0400
Message-ID<roy-1CB868.21340005102013@news.panix.com>
In reply to#56228
In article <fbc6e512-88fa-4de0-80d3-6757fcc52af4@googlegroups.com>,
 Harvey Greenberg <hjgreenberg@gmail.com> 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?

I hate to recommend it (since it's bad practice for a number of 
legitimate reasons), but passing your string to eval() will get you what 
you want.

It's also very close to being valid JSON syntax, the only difference 
being the use of single instead of double quotes.  You might want to 
just turn it into JSON by substituting the right kind of quotes.

json.loads("[{'a':1, 'b':2}, [1,2,3], 10]".replace("'", '"'))
[{u'a': 1, u'b': 2}, [1, 2, 3], 10]

[toc] | [prev] | [next] | [standalone]


#56239

FromTerry Reedy <tjreedy@udel.edu>
Date2013-10-06 02:24 -0400
Message-ID<mailman.769.1381040680.18130.python-list@python.org>
In reply to#56228
On 10/5/2013 9:08 PM, Harvey Greenberg wrote:
> I am looping as for L in file.readlines(), where file is csv.

I believe 'for L in file:' does the same, more efficiently, even in 2.7.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#56272

FromHarvey Greenberg <hjgreenberg@gmail.com>
Date2013-10-06 09:57 -0700
Message-ID<236e2bfa-94a2-4c6b-a6e7-7105370375fc@googlegroups.com>
In reply to#56228
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!

[toc] | [prev] | [next] | [standalone]


#56273

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-06 18:24 +0100
Message-ID<mailman.789.1381080286.18130.python-list@python.org>
In reply to#56272
On 06/10/2013 17:57, Harvey Greenberg wrote:
> 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!
>

Very pleased to know, but if you need to post again would you be kind 
enough to read this first, thanks 
https://wiki.python.org/moin/GoogleGroupsPython

-- 
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#56274

FromRavi Sahni <ganeshsahni07@gmail.com>
Date2013-10-06 23:08 +0530
Message-ID<mailman.790.1381082860.18130.python-list@python.org>
In reply to#56272
On Sun, Oct 6, 2013 at 10:27 PM, Harvey Greenberg <hjgreenberg@gmail.com> wrote:
> 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!

Which method working?
Literal_eval method? JSON method? Some third method?

[I am newbie so interested. Please to excuse!!]
-- 
Ravi

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web