[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

newbie question: converting csv to another dialect

Albert-jan Roskam

2/15/2008 11:11:00 AM

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=Ahu06i62sR8HDtDypa...

2 Answers

Chris

2/15/2008 11:28:00 AM

0

On Feb 15, 1:11 pm, Albert-jan Roskam <fo...@yahoo.com> wrote:
> 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=Ahu06i62sR8HDtDypa...

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.

Bruno Desthuilliers

2/15/2008 12:39:00 PM

0

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