PDA

View Full Version : What is the best way to do this in python?



SGC
September 3rd, 2005, 01:18 AM
If I have a large text file that has something like this:


text=Kubuntu
color=Red
font=Arial
...


What the best way to know the value of color,text,font,etc?
I usually uses string.find to locate "color=" then save the text after that in a variable, but I think this is not an effient way to do it.

vasdee
September 3rd, 2005, 02:47 AM
i reckon use a while loop on each line, something like this


vals = {}
for line in inputfile.readlines():
info = line.split('=')
vals['info[0]'] = info[1]


then you should be able to look up the values by vals['color'], vals['font'] etc

SGC
September 3rd, 2005, 03:15 AM
i reckon use a while loop on each line, something like this


vals = {}
for line in inputfile.readlines():
info = line.split('=')
vals['info[0]'] = info[1]


then you should be able to look up the values by vals['color'], vals['font'] etc
Thank you very much for your help

wtd
September 5th, 2005, 05:49 PM
i reckon use a while loop on each line, something like this


vals = {}
for line in inputfile.readlines():
info = line.split('=')
vals['info[0]'] = info[1]


then you should be able to look up the values by vals['color'], vals['font'] etc
If you're using Python 2.4 or later that can be simplified a tad.


vals = {}
for line in inputfile:
info = line.split('=')
vals[info[0]] = info[1]

Or perhaps:


vals = dict(line.split('=', 2) for line in inputFile)

:)

SGC
September 7th, 2005, 07:22 AM
vals = dict(line.split('=', 2) for line in inputFile)
Thanks, I like this one.

wtd
September 7th, 2005, 08:32 PM
Just one note... I'm fairly sure the 2 should be 1 in that call of split.

Also worth noting is that there's more than simply a stylistic difference between:


for line in inputFile.readlines()

And:


for line in inputFile

The former reads in all lines, then iterates over them. The latter reads through them one at a time without creating an intermediate list. This can be very useful if you're dealing with very large files, and especially if at some point you plan to break out of the loop before you get to the end of the file.

ArBaDaCarBa
September 9th, 2005, 03:21 PM
Just one note... I'm fairly sure the 2 should be 1 in that call of split.

Also worth noting is that there's more than simply a stylistic difference between:


for line in inputFile.readlines()

And:


for line in inputFile

The former reads in all lines, then iterates over them. The latter reads through them one at a time without creating an intermediate list. This can be very useful if you're dealing with very large files, and especially if at some point you plan to break out of the loop before you get to the end of the file.

Very usefull info. I'll remember this, thanks !

UbuWu
October 1st, 2005, 09:06 PM
What does info = line.split('=') do when there is no '=' in the line? Is the line ignored or is it also stored in memory?

jrbj
October 1st, 2005, 11:44 PM
What does info = line.split('=') do when there is no '=' in the line? Is the line ignored or is it also stored in memory?

The line is not ignored. It is just not split becaused the specified delimiter is not present in that line.

For example:

>>> line1 = 'font=Verdana'
>>> line1.split('=')
['font', 'Verdana']


>>> line2 = 'font Monaco'
>>> line2.split('=')
['font Monaco']