working with csv module in python

Roland Koebler r.koebler at yahoo.de
Wed Feb 20 05:26:01 EST 2013


Hi,

On Wed, Feb 20, 2013 at 10:50:54AM +0100, inshu chauhan wrote:
> I have 10 simple text files with 3 columns x,y,z delimited by "space". I am
> trying to combine these 10 files to get a single text file.
> 
> Eg. of data in 10 files is
> 299 446 2
Do you only want to concat the files, or do you want to parse/mangle
them?

If you only want to concat the files, I would use some shell-tools,
like "cat" on Linux or "copy" on Windows, so

copy C:\Users\inshu.chauhan\Desktop\ForModel_600\*.arff C:\Users\inshu.chauhan\Desktop\test2.arff

should do it.

> Can in some some way I set delimiter to NULL as the prog gives me error if
> I do so.
Of course -- a CSV without a delimiter doesn't make any sense.

> I dont why there is space between the attribute of first column in
> reading and there is space between every row too..
Because there's a "split()" missing in your code. You currently tell the
CSV-writer to write the columns 2,9,9, , , ,4,4,6, , , ,2 as
space-separated CSV. So, try something like
rows = [r.split() for r in open(f, "r").readlines()]

> Or can I merge these text files without using csv module , directly in
> python ?
If you don't need to parse/mangle the contents, you don't need the csv
module. Simple open the resulting file for writing, and then read out
the source files and write their contents into the resulting file.


regards
Roland



More information about the Python-list mailing list