newbie question

colarte at telesat.com.co colarte at telesat.com.co
Thu Sep 4 16:14:24 EDT 2003


I think you can do something like this, the key of the "parsing" is using the 
split function from the string module, 


<file data.dat>
aa juan
bb pedro
cc pablo
</file data.dat>


<PythonCode>
import string
myfile = "./data.dat"

liststr1 = []
liststr2 = []

for line in open(myfile, 'r').readlines():
    arrTmp = string.split(line, " ")
    liststr1.append(arrTmp[0])
    liststr2.append(arrTmp[1][:-1])  #  arrTmp[1][:-1] remove \n

print liststr1, liststr2

</PythonCode>

.i hope this helps .

Camilo Olarte



Mensaje citado por Christoph Lehmann <christoph.lehmann at gmx.ch>:

> hi
> I need to read in a textfile, where in each line are two strings:
> 
> e.g
> 
> aa 1
> bb 0
> cc 1
> aa 0
> dd 0
> 
> 
> how can I read in each line and put the first string into one variable,
> the second into another variable
> 
> with just one string per line, I did:
> 
> f = open(myfile, 'r')
> for filename in f.readlines():
>     mylist.append(string.rstrip(filename))                   #remove \n
> or \r\n
> f.close()
> 
> many thanks
> -- 
> Christoph Lehmann <christoph.lehmann at gmx.ch>
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

Telesat, más fácil...más Internet.
http://www.telesat.com.co/





More information about the Python-list mailing list