473,807 Members | 2,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13135
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...@y ahoo.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
2851
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 filesystem Windows doesn't recognise), store it as a file, modify it and dump it back to the card. Currently, in version 0.001 of my program, I'm just calling "dd for Windows" using ShellExecute - this works perfectly, but obviously this isn't...
1
2929
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
3505
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 written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to customer_record SETUP PROGRAM
2
2821
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 anyone have any ideas? I've noticed that .NET runs slower for my other programs too. thanks in advance for any suggestions.
7
6110
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 --------/--------/--------/--------/--------/--------/ // field_width=8 For example, I position my file pointer at the begining of the 4th fileld lets say 25th character(3*(field_width)+1) and when I try to
3
18966
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 original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
1
2130
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, write my data, and then close the file. The generated file itself is a text file that is formatted either comma separated or tab separeted. My problem is that the file my app generates can and probably will be opened by the user in MS Excel....
6
5278
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
1111
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 option to hit a button and see how the payments break down and then offer a print button to print these results out nice and neat. 1- Can I throw the results into a large Multi Line Text box in my Form and print it off okay that way? Or should I (and...
0
9720
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.