473,320 Members | 1,865 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,320 software developers and data experts.

formatting number in the CSV module

Hi,

I need to output numbers to a csv file and use the new csv module in
python2.3:

"
import csv
data = [[ 0.321524523,0.5243523],[0.54635643,0.62776]]
w = csv.writer(file("out.csv","w"))
w.writerows(data)
"

However, I would like to format the number so that only the first 2
decimals are stored in the csv file.

Is it possible to specify a formatting command like "%.2f" to the csv
writer ?

Seb

ps: data in the real application is a BIG numeric array...
Jul 18 '05 #1
2 16085
Sebastien de Menten wrote:
Hi,

I need to output numbers to a csv file and use the new csv module in
python2.3:

"
import csv
data = [[ 0.321524523,0.5243523],[0.54635643,0.62776]]
w = csv.writer(file("out.csv","w"))
w.writerows(data)
"

However, I would like to format the number so that only the first 2
decimals are stored in the csv file.

Is it possible to specify a formatting command like "%.2f" to the csv
writer ?


I suggest wrapping your data into a generator:

import csv
import sys
data = [
[ 0.321524523,0.5243523],
[0.54635643,0.62776]
]

def formatdata(data):
for row in data:
yield ["%0.2f" % v for v in row]

w = csv.writer(sys.stdout)
w.writerows(formatdata(data))

Peter
Jul 18 '05 #2

Sebastien> I need to output numbers to a csv file and use the new csv
Sebastien> module in python2.3:

Sebastien> "
Sebastien> import csv
Sebastien> data = [[ 0.321524523,0.5243523],[0.54635643,0.62776]]
Sebastien> w = csv.writer(file("out.csv","w"))
Sebastien> w.writerows(data)
Sebastien> "

Sebastien> However, I would like to format the number so that only the
Sebastien> first 2 decimals are stored in the csv file.

There's nothing built into the csv module. You can format the floats before
passing them to the writer however:
import csv
data = [[ 0.321524523,0.5243523],[0.54635643,0.62776]]
w = csv.writer(file("out.csv","w"))
for row in data: ... row = ["%.2f"%f for f in row]
... w.writerow(row)
...

montanaro:tmp% cat out.csv
0.32,0.52
0.55,0.63

Skip

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jan Decaluwe | last post by:
Hi: There is a difference between exception info formatting by the interpreter versus the traceback module. For example, say we define an exception Error in file module.py: $ python Python...
8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
6
by: Stefan Behnel | last post by:
Hi! The logging module nicely prepends each line with a formatted date. However, I'm not interested in the actual date but only in the number of milliseconds that passed since the start of the...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
4
by: John Sutor | last post by:
I need some code that, on each keyup event, will take all of the numbers typed into the text box and format as they type to look like this 100 1,000 10,000 100,000 1,000,000 John S
1
by: coleenholley | last post by:
I'm getting frustrated with trying to format a datagrid that is entirely populated dynamically using a class module written in VB .Net (No C# used!) We connect to a DB2 database using an RPC that is...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
4
by: leen | last post by:
I'm working in Access2000 and am having trouble finding a way to take imported "OLEPath" field values in my database and filter out or eliminate all the alpha text so we only get numeral values,...
12
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm doing a web app in VB/Dot Net 2.0. I'm probably a bit rusty and I have no experience using the repeater control. I have a user control I've created with multiple properties. I've created a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.