Newbie problem

Matt Goodall matt at pollenation.net
Thu Nov 27 12:17:23 CET 2003


Angelo Secchi wrote:

>Hallo,
>
>I have the following problem. I'm trying to clean a database whose lines
>look like:
>
>"2/G DM"        "HOE"   0       "R5D2"  1       0       0       0      
>
>in particular I need to remove the ".
>
>Here my code
>
>def wash(a) :
>    a=string.replace(a,'"','')
>    return a
>
>data=file("prova.txt",'r')
>data_clean=file("prova_clean.txt",'w')
>
>temp_string = data.readline()
>while len(temp_string) != 0:
>    temp_field=string.split(temp_string,'\t')
>    print temp_field
>    temp_field=map(wash,temp_field)
>    print temp_field
>    data_clean.write(temp_field)
>    temp_string = data.readline()
>
>data.close()
>data_clean.close()
>
>Of course I receive an error in line "data_clean.write(temp_field)"
>saying "TypeError: argument 1 must be string or read-only character
>buffer, not list". I'm a newbie and reading tutorials around I was not
>able to find the best solution to my problem. Could anybody help me?
>  
>
You presumably need to output the result with tab delimiters as in the 
source file so you need to do:

 >>> data_clean.write('\t'.join(temp_field))

I should point out that your code could be greatly simplified. You could 
write it something like this (untested):

 >>> data=file("prova.txt",'r')
 >>> data_clean=file("prova_clean.txt",'w')
 >>> for line in data:
 >>>    line = '\t'.join([s.replace('"','') for s in line.split('\t')])
 >>>    data_clean.write(line)

You may prefer to continnue using map() rather than a list 
comprehension, that's up to you.

My final point (I promise!) is that this looks remarkably like a tab 
delimited CSV file. Are you aware of the standard CSV module in Python 
2.3? If you're using an older version of Python then there is also 
Object Craft's CSV parser.

Hope this helps.

Cheers, Matt

-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt at pollenation.net
t: +44 (0)113 2252500







More information about the Python-list mailing list