Converting a text file to a string?
Robin Becker
robin at jessikat.fsnet.co.uk
Sat Sep 1 12:27:14 EDT 2001
In article <QC7k7.5474$QG.2234404 at monolith.news.easynet.net>, eif
<eif at ukonline.co.uk> writes
>can anyone please help me out here? - im running a python cgi script that
>outputs a database in a text file format something that looks like this:
>
>999347857||9/1/2001 at 13:37:37|016354|jim o
>shea|jim at mmu.ac.uk|126|0161025658
>999347943||9/1/2001 at 13:39:03|223654|roy
>saberton|roy at mmu.ac.uk|201|0161236510
>999347943||9/1/2001 at 13:39:03|223654|roy
>saberton|roy at mmu.ac.uk|201|0161236510
>
>I can open and read this file, but the problem is I want to convert this
>file into a list - in order to access and manipulate each individual
>element. can anyone tell me how to do this or am I going the wrong way about
>the problem?
>
>cheers
well with your sample
L = open('/tmp/tdb.txt','r').readlines()
R = []
for i in xrange(0,len(L),2):
R.append((L[i].strip()+L[i+1].strip()).split('|'))
from pprint import pprint
pprint(R)
produced
C:\Tmp>tryit.py
[['999347857',
'',
'9/1/2001 at 13:37:37',
'016354',
'jim oshea',
'jim at mmu.ac.uk',
'126',
'0161025658'],
['999347943',
'',
'9/1/2001 at 13:39:03',
'223654',
'roysaberton',
'roy at mmu.ac.uk',
'201',
'0161236510'],
['999347943',
'',
'9/1/2001 at 13:39:03',
'223654',
'roysaberton',
'roy at mmu.ac.uk',
'201',
'0161236510']]
C:\Tmp>
--
Robin Becker
More information about the Python-list
mailing list