Newbie problem

A

Angelo Secchi

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?

I also read in this mailing list that there exists a specific list for
newbie (called python tutor mailing list) but I was not able to find it.
Any suggestions?

Thanks

Angelo
 
P

Peter Otten

Angelo said:
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


Here's how to convert the list of strings into a tab-separated string:

"\t".join(temp_field)

Note that the functions in the string module are available as string
methods, for example instead of

string.replace(s, old, new)

you'd rather write

s.replace(old, new)

A more readable way to iterate over the lines in a file is:

for temp_string in data:
# process temp_string here

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top