newbie question: converting csv to another dialect

A

Albert-jan Roskam

Hi all,

I have a csv file with tab as a delimiter. I want to
convert it into one that is comma delimited. I changed
the regional settings on my computer to US.

At first I thought I could use the CSV module for
this, by reading the csv file, registering the new
(desired = comma-delimited) dialect, and writing it.

Another option (which may be easier) I tried was:
f = open('d:/temp/myfile.csv', ' rw')
for row in f:
row.replace("\t",",")

Which is a start, but doesn't do the entire job.
Could somebody help me with this please?

Thanks very much in advance!

Albert-Jan


____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
 
C

Chris

Hi all,

I have a csv file with tab as a delimiter. I want to
convert it into one that is comma delimited. I changed
the regional settings on my computer to US.

At first I thought I could use the CSV module for
this, by reading the csv file, registering the new
(desired = comma-delimited) dialect, and writing it.

Another option (which may be easier) I tried was:
f = open('d:/temp/myfile.csv', ' rw')
for row in f:
row.replace("\t",",")

Which is a start, but doesn't do the entire job.
Could somebody help me with this please?

Thanks very much in advance!

Albert-Jan

____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

easiest way would be to split and then join imo

for line in input_file:
output_file.write(','.join(line.split('\t')) )

If you decide on quote encapsulated comma-delimeted, don't forget to
add additional quotes to the start and end of the line.
Also, if you open the file in OO Calc and you choose to resave it you
can choose the delimiter.
 
B

Bruno Desthuilliers

Albert-jan Roskam a écrit :
Hi all,

I have a csv file with tab as a delimiter. I want to
convert it into one that is comma delimited. I changed
the regional settings on my computer to US.

At first I thought I could use the CSV module for
this, by reading the csv file, registering the new
(desired = comma-delimited) dialect, and writing it.

That's what I'd do indeed.
Another option (which may be easier)

Hum... Depends on the effective csv dialect used. So-called csv files
are not necessarily trivial to handle properly.
I tried was:
f = open('d:/temp/myfile.csv', ' rw')
for row in f:
row.replace("\t",",")

Which is a start, but doesn't do the entire job.

// without proper error handling
source = '/path/to/source.csv'
dest = source + '.new'

fin = open(source, 'r')
fout = open(dest, 'w')

for row in source:
// let's hope there's no ',' anywhere in source...
fout.write(row.replace("\t", ","))

fin.close()
fout.close()
os.rename(dest, source)
Could somebody help me with this please?

yes : forget the above code and use the csv module.

HTH
 

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
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top