LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   reading fields from files in python without \t \n (https://www.linuxquestions.org/questions/programming-9/reading-fields-from-files-in-python-without-%5Ct-%5Cn-124780/)

dumpthecore 12-10-2003 10:39 PM

reading fields from files in python without \t \n
 
PyGurus,

When I do readlines onto a file, with several columns, it returns it with
data\t\n. I would like to have the first column in a array, second one in another
and so on.

First is that I would like to strip these characters \t\n,
Second allocating the column to an array.

finp=open("test1.dat","r")
lines=finp.readlines()

I would appreciate some pointers.

Thanks
DTC.

yuray 12-11-2003 04:57 AM

read doc on module string
import string
>>> x=open('/etc/passwd','r')
>>> s=x.readlines()
>>> s
['root:x:0:0:root:/root:/bin/bash\n', 'daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n', 'bin:x:2:2:bin:/bin:/bin/sh\n',
>>> string.strip(s[0])
'root:x:0:0:root:/root:/bin/bash'
>>>


All times are GMT -5. The time now is 03:40 PM.