Converting a text data file from positional to tab delimited.
Emile van Sebille
emile at fenx.com
Tue Mar 13 09:31:36 EST 2001
The obvious change is to use a list to collect the fields, then string.join
to build the record. If you're on 1.5.2, import string first.
See notes below. HTH
Emile van Sebille
emile at fenx.com
"Lee Joramo" <lee at joramo.com> wrote in message
news:B6D37840.2124%lee at joramo.com...
> I am looking for suggestions to speed up the process of converting a large
<snip>
> inFile = open('rawdata.dat', 'r')
> outFile = open('delimted.dat', 'w')
> while 1:
> lines = inFile.readlines(1000)
> if not lines: break
> for line in lines:
replace this:
> delimitedLine = ""
with this:
delimitedLine = []
> delimit = ""
> for field in layout:
> #
> #can this loop be improved??
> #
replace this:
> fieldValue = line[field[1]:field[2]]
> delimitedLine = delimitedLine + delimit + fieldValue
> delimit = "\t"
with this:
outrec.append(line[field[1]:field[2]])
and then replace this:
> outFile.write(delimitedLine+"\n")
with this:
outfile.write("\t'.join(outrec)+"\n")
or with 1.5.2:
outfile.write(string.join(outrec,'\t')+"\n")
> inFile.close()
> outFile.close()
> del lines
>
More information about the Python-list
mailing list