473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

String from File -> List without parsing

Hi,

given the dynamic nature of python I assume that there is an elegant
solution for my problem, but I did not manage to find it.

I have a file that contains for example on line:

['147', '148', '146']

when I read the file

f = file("I050901.ids").readlines()

I have a string

f[0] == "['147', '148', '146']"

How can I turn this string into a list

li == ['147', '148', '146']

without parsing?

--
Greg
Sep 4 '05 #1
3 1529
Gregor,

You want to use eval():

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
eval('[3,54,5]') [3, 54, 5]

Cheers,
J.F.

Gregor Horvath wrote: Hi,

given the dynamic nature of python I assume that there is an elegant
solution for my problem, but I did not manage to find it.

I have a file that contains for example on line:

['147', '148', '146']

when I read the file

f = file("I050901.ids").readlines()

I have a string

f[0] == "['147', '148', '146']"

How can I turn this string into a list

li == ['147', '148', '146']

without parsing?

--
Greg

Sep 4 '05 #2
Gregor Horvath wrote:
Hi,

given the dynamic nature of python I assume that there is an elegant
solution for my problem, but I did not manage to find it.

I have a file that contains for example on line:
['147', '148', '146']
when I read the file
f = file("I050901.ids").readlines()
Y3K bug alert :-)
I have a string
f[0] == "['147', '148', '146']"
The last (or sole) line is highly likely to be "['147', '148',
'146']\n". If there are multiple lines in the file, all but the last
will definitely have "\n" at the end.
How can I turn this string into a list
li == ['147', '148', '146']
without parsing?


Something like this:
def munch(astrg): .... return [x[1:-1] for x in astrg.rstrip("\n")[1:-1].split(", ")]
.... munch("['147', '148', '146']") ['147', '148', '146'] munch("['147', '148', '146']\n") ['147', '148', '146'] munch("['147']\n") ['147'] # Warning: various flavours of "nothing" give the same result:
munch("['']\n") [''] munch("[]\n") [''] munch("\n") ['']


This assumes that the contents are no more complicated than in your example.

Some notes:

(1) You neither want nor need to use eval.

(2) What is creating files like that? If it is a Python script, consider
writing them using the csv module; that way, other software can read
them easily.

HTH,
John
Sep 4 '05 #3
John Machin wrote:
f = file("I050901.ids").readlines()
Y3K bug alert :-)


but then there is Python 3000 and Hurd, which solves all problems of
this universe :-)
Something like this:
>>> def munch(astrg):
... return [x[1:-1] for x in astrg.rstrip("\n")[1:-1].split(", ")]


Thanks!
(1) You neither want nor need to use eval.
I wanted to avoid parsing, but when parsing is an 1 liner, I think its
the better solution.

(2) What is creating files like that? If it is a Python script, consider
writing them using the csv module; that way, other software can read
them easily.


Its a Python script.
But the file is not intended to be used by another sotware, although it
should be readable by humans. I think the python syntax is better human
readable than csv.

After all the sense of this file is to make a list persistant quickly
and easily and human readable. Maybe pickleing is a third option?

--
Gregor
Sep 5 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
12
by: Hp | last post by:
Hi All, Thanks a lot for all your replies. My requirement is as follows: I need to read a text file, eliminate certain special characters(like ! , - = + ), and then convert it to lower case and...
6
by: tshad | last post by:
In my User control, I tried to do this: *************************************************************************** <Script runat="server"> Public ClientName As String = "<!-- #include file =...
14
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
2
by: laksh2007 | last post by:
i want to search and replace strings present in a file (entire file).. i have wirtten cod eto do this.. but it is not working as expected.. code is : import java.io.*; import java.util.*;
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
1
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.