By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Writing a nice formatted csv file

P: n/a
Hi all,

I use the csv module of Python to write a file. My code is of the
form :

cw = csv.writer(open("out.txt", "wb"))
cw.writerow([1,2,3])
cw.writerow([10,20,30])

And i get an out.txt file looking like:
1,2,3
10,20,30

Whereas what I'd like to get is:
1, 2, 3,
10, 20, 30

which is more readable.

Can anybody help me to do so ?

Thanks,

Cédric

May 2 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On May 2, 8:14 am, redcic <cedric.lou...@gmail.comwrote:
Hi all,

I use the csv module of Python to write a file. My code is of the
form :

cw = csv.writer(open("out.txt", "wb"))
cw.writerow([1,2,3])
cw.writerow([10,20,30])

And i get an out.txt file looking like:
1,2,3
10,20,30

Whereas what I'd like to get is:
1, 2, 3,
10, 20, 30

which is more readable.

Can anybody help me to do so ?

Thanks,

Cédric
cvs files are constructed for efficient processing not formatting so
that you can read them easier. If you want a formatted file, then
construct one.

May 2 '07 #2

P: n/a
On 2 May 2007 07:14:04 -0700, redcic <ce***********@gmail.comwrote:
And i get an out.txt file looking like:
1,2,3
10,20,30
Whereas what I'd like to get is:
1, 2, 3,
10, 20, 30
which is more readable.
The idea behind csv module is to produce and read csv files that are
"machine readable" rather than "human readable", so I think you should
write the file "by hand" to take into account those whitespace.

--
Sebastián Bassi (セバスティアン)
Diplomado en Ciencia y TecnologÃ*a.
GPG Fingerprint: 9470 0980 620D ABFC BE63 A4A4 A3DE C97D 8422 D43D
Club de la razón (www.clubdelarazon.org)
May 2 '07 #3

P: n/a
Well then how can I format a file ?

Thanks for your help,

Cédric

On 2 mai, 16:26, 7stud <bbxx789_0...@yahoo.comwrote:
On May 2, 8:14 am, redcic <cedric.lou...@gmail.comwrote:
Hi all,
I use the csv module of Python to write a file. My code is of the
form :
cw = csv.writer(open("out.txt", "wb"))
cw.writerow([1,2,3])
cw.writerow([10,20,30])
And i get an out.txt file looking like:
1,2,3
10,20,30
Whereas what I'd like to get is:
1, 2, 3,
10, 20, 30
which is more readable.
Can anybody help me to do so ?
Thanks,
Cédric

cvs files are constructed for efficient processing not formatting so
that you can read them easier. If you want a formatted file, then
construct one.

May 2 '07 #4

P: n/a
On Wed, May 02, 2007 at 07:28:32AM -0700, redcic wrote:
Well then how can I format a file ?
for row in rows:
print "".join([ "%-6s" % ("%d," % cell) for cell in row ])

The "%-6s" formats each column to be no less than six characters long;
the "%d," formats the number with a comma after it.

This won't be quite what you want, since you've comma-aligned all of the
fields after the first, but it should be readable.
Whereas what I'd like to get is:
1, 2, 3,
10, 20, 30
which is more readable.
<snip>

cvs files are constructed for efficient processing not formatting so
that you can read them easier. If you want a formatted file, then
construct one.
<snip>
May 2 '07 #5

P: n/a
On 2 May, 15:14, redcic <cedric.lou...@gmail.comwrote:
Hi all,

I use the csv module of Python to write a file. My code is of the
form :

cw = csv.writer(open("out.txt", "wb"))
cw.writerow([1,2,3])
cw.writerow([10,20,30])

And i get an out.txt file looking like:
1,2,3
10,20,30

Whereas what I'd like to get is:
1, 2, 3,
10, 20, 30

which is more readable.

Can anybody help me to do so ?
How about pre-formatting the columns before hand before using
something like:

# List of formatting to apply to each column (change this to suit)
format = ['%03d', '%10s', '%10s']

data = [1, 10, 100]
print [fmt % d for fmt,d in zip(format,data)]

Results in: ['001', ' 10', ' 100']

Then write that using the CSV module.

hth

Jon.

May 2 '07 #6

P: n/a
Whereas what I'd like to get is:
1, 2, 3,
10, 20, 30
(without trying this myself first...)
You might try setting up a csv dialect with a delimiter of ',\t' then
using a reader that can set tab stops for display.

-Dave
May 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.