473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Writing a nice formatted csv file

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
6 13092
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Benjamin de Waal | last post by:
Hey all, I'm trying to figure out how to directly write to a device in Windows. Basically, what I'm wanting to do is create an image of a device (specifically, a CompactFlash card that uses a...
1
by: Dan | last post by:
How would I write an XmlDocument to a file so that each node is properly indented and followed by a carriage return? Thanks...Dan
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
2
by: C.H. | last post by:
Hello, I am writing to a .txt file. Lots of data, about 1.1MB. It only took less than a second for VB6 to do so, but it took VB.NET one minute!! I am using a simple fileopen and print. Does...
7
by: utab | last post by:
Hi there, I am trying to read from a file and at the same time change certain fields of the same field, there are 6 fields in this file like 1 2 3 4 5 6...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
1
by: RahimAsif | last post by:
I am designing a data acquisition program that peridically collects data from a panel and writes to a file. The way I am doing it right now, every time I have data from the panel, I open the file,...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: tvance929 | last post by:
Hey guys, Can someone turn me onto a tutorial or at least give me some begining advice how to accomplish the following: I am making an Loan Payment calculator. I want to give the user the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.