Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:file': 0.07; '[1,': 0.09; 'parsing': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'jan': 0.12; '-tkc': 0.16; '2.7.3': 0.16; '3],': 0.16; 'ast': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'looping': 0.16; 'wrote:': 0.18; 'split': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'subject:list': 0.30; 'work.': 0.31; 'file': 0.32; 'subject:from': 0.34; 'convert': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'list': 0.37; 'list,': 0.38; '2nd': 0.60; 'first': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'received:50.22': 0.84; 'subject:read': 0.84; '2013,': 0.91; 'items,': 0.91 Date: Sat, 5 Oct 2013 20:24:39 -0500 From: Tim Chase To: Harvey Greenberg Subject: Re: how to read list from file In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: none Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381022585 news.xs4all.nl 15931 [2001:888:2000:d::a6]:60923 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56230 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