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

[NEWBIE] csv to excel format problem

MM
Hi to all,

I'm trying to import a tab separated values file onto Excel with the
following script:

import csv
from pyExcelerator import *

w = Workbook()
worksheet = w.add_sheet('sim1')

def writeExcelRow(worksheet, lno, columns):
style = XFStyle()
style.num_format_str = '0.00E+00'
cno = 0
for column in columns:
worksheet.write(lno, cno, column,style)
cno = cno + 1

nrow = 0
csvfile = file('res1.txt','r')
csvreader = csv.reader(csvfile, delimiter='\t')

for line in csvreader:
writeExcelRow(worksheet,nrow,line)
nrow += 1

csvfile.close()
w.save('numbers.xls')

All goes well and the resulting file "numbers.xls" has all the numbers
in the right place....

The problem is that excel sees the numbers as text and gives the error
"numbers stored as text" that I have to correct manually.

The file res1.txt has the structure of a tab separated values of
floating point numbers.

Thank you for the help.

Marco
Oct 14 '08 #1
3 3353
MM
On 14 Ott, 12:03, MM <marco.me...@gmail.comwrote:
Hi to all,

I'm trying to import a tab separated values file onto Excel with the
following script:

import csv
from pyExcelerator import *

w = Workbook()
worksheet = w.add_sheet('sim1')

def writeExcelRow(worksheet, lno, columns):
* style = XFStyle()
* style.num_format_str = '0.00E+00'
* cno = 0
* for column in columns:
* * worksheet.write(lno, cno, column,style)
* * cno = cno + 1

nrow = 0
csvfile = file('res1.txt','r')
csvreader = csv.reader(csvfile, delimiter='\t')

for line in csvreader:
* * writeExcelRow(worksheet,nrow,line)
* * nrow += 1

csvfile.close()
w.save('numbers.xls')

All goes well and the resulting file "numbers.xls" has all the numbers
in the right place....

The problem is that excel sees the numbers as text and gives the error
"numbers stored as text" that I have to correct manually.

The file res1.txt has the structure of a tab separated values of
floating point numbers.

Thank you for the help.

Marco
I've found the answer by myself...
Maybe for you it would be simple!
for line in csvreader:
writeExcelRow(worksheet,nrow,map(float,line))
nrow += 1
Thank you anyway
Oct 14 '08 #2
On Oct 15, 3:49*am, MM <marco.me...@gmail.comwrote:
On 14 Ott, 12:03, MM <marco.me...@gmail.comwrote:
I'm trying to import a tab separated values file onto Excel with the
following script:
import csv
from pyExcelerator import *
Consider using xlwt instead ... see http://pypi.python.org/pypi/xlwt/

xlwt is an actively-maintained (as recently as yesterday) fork of
pyExcelerator ... bugs fixed, functionality enhancements, speed-ups.

Also consider reading/joining the python-excel newsgroup/list at
http://groups.google.com.au/group/python-excel
>
w = Workbook()
worksheet = w.add_sheet('sim1')
def writeExcelRow(worksheet, lno, columns):
* style = XFStyle()
* style.num_format_str = '0.00E+00'
It is generally a good idea NOT to do things once per iteration when
you can do it only once; you save CPU time and maybe memory. In the
case of XFs in Excel spreadsheets, it's a VERY good idea ... there's a
maximum of about 4000 XFs in Excel (up to Excel 2003, at least).
pyExcelerator's method of avoiding creating unwanted XFs depends
partly on address comparison instead of value comparison, and can thus
go pear-shaped in scenarios more complicated (and thus harder to
debug) than yours.
* cno = 0
* for column in columns:
* * worksheet.write(lno, cno, column,style)
* * cno = cno + 1
nrow = 0
csvfile = file('res1.txt','r')
Another good habit to acquire: always use 'rb' to ensure that the file
is opened in binary mode.
csvreader = csv.reader(csvfile, delimiter='\t')
for line in csvreader:
* * writeExcelRow(worksheet,nrow,line)
* * nrow += 1
csvfile.close()
w.save('numbers.xls')
All goes well and the resulting file "numbers.xls" has all the numbers
in the right place....
The problem is that excel sees the numbers as text and gives the error
"numbers stored as text" that I have to correct manually.
The file res1.txt has the structure of a tab separated values of
floating point numbers.
Thank you for the help.
Marco

I've found the answer by myself...
Maybe for you it would be simple!

for line in csvreader:
* * writeExcelRow(worksheet,nrow,map(float,line))
* * nrow += 1
.... or a more general solution if you have a mixture of numbers,
dates, text :-)

Cheers,
John

Oct 14 '08 #3
Hello Marco and welcome to the wonderful world of Python,

Your problem is that the file is a text file so the values you are reading
are text which you then write to the Excel sheet.

So you need to convert the text value to a float value. Now the Big Cahonas
has already been there so it's included:

float( [x])

Convert a string or a number to floating point. If the argument is a
string, it must contain a possibly signed decimal or floating point number,
possibly embedded in whitespace. Otherwise, the argument may be a plain or
long integer or a floating point number, and a floating point number with
the same value (within Python's floating point precision) is returned. If
no argument is given, returns 0.0.
Note: When passing in a string, values for NaN and Infinity may be
returned, depending on the underlying C library. The specific set of
strings accepted which cause these values to be returned depends entirely
on the C library and is known to vary

If you do this change it might work ;-)
Add these functions:
def isNaN(x):
return isinstance(x, float) and x!=x
def isInf(x):
return !isNaN(x) && isNaN( (x) - (x) )

in writeExcelRow change:
for column in columns:
fcolumn = float(column)
if(isNaN(fcolumn) or isInf(fcolumn)):
# do some error handling
else:
worksheet.write(lno, cno, column,style)
cno = cno + 1

I'm sure that there are smarter ways of doing this and someone will
probably point them out ;-)

// Anders
--
English is not my first, or second, language
so anything strange, or insulting, is due to
the translation.
Please correct me so I may improve my English!

On Tue, 14 Oct 2008 03:03:52 -0700 (PDT), MM wrote:
Hi to all,

I'm trying to import a tab separated values file onto Excel with the
following script:

import csv
from pyExcelerator import *

w = Workbook()
worksheet = w.add_sheet('sim1')

def writeExcelRow(worksheet, lno, columns):
style = XFStyle()
style.num_format_str = '0.00E+00'
cno = 0
for column in columns:
worksheet.write(lno, cno, column,style)
cno = cno + 1

nrow = 0
csvfile = file('res1.txt','r')
csvreader = csv.reader(csvfile, delimiter='\t')

for line in csvreader:
writeExcelRow(worksheet,nrow,line)
nrow += 1

csvfile.close()
w.save('numbers.xls')

All goes well and the resulting file "numbers.xls" has all the numbers
in the right place....

The problem is that excel sees the numbers as text and gives the error
"numbers stored as text" that I have to correct manually.

The file res1.txt has the structure of a tab separated values of
floating point numbers.

Thank you for the help.

Marco
Oct 16 '08 #4

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

Similar topics

3
by: Jeremy | last post by:
Hi, I have a webpage that is taking input from a form and using it as criteria to select data out of an sql database. The page displays an html table with the results. The user can then click a...
2
by: Kumar | last post by:
Hi Folks, I have a question regarding my windows c# application. This application just reads MS Excel file and puts the data in to sql server database. In that excel file ,it has one named cell...
8
by: John Brock | last post by:
I am creating an Excel workbook using VB.NET, and have run into a problem. Excel at times insists on reformatting data that I enter into cells, e.g., converting "01234" to "1234", and this screws...
6
by: Kevin Humphreys | last post by:
Hi There, I am trying to export a recordset to an excel file using the Content Type below in the header. Response.ContentType = "application/vnd.ms-excel" Which works fine however the...
6
by: Piotr | last post by:
Hi, I have following problem: I use a form in excel to send data into mysql server, everything is ok unless I have to deal with decimals or data fields, this simple are not recognized. For...
1
by: desi.american | last post by:
I have a dynamically generates ASPX page with tables and data. Depending on user selection, the same page can be viewed as a simple web page (rendered in HTML) or as an excel spreadsheet. If the...
18
by: gonzlobo | last post by:
No, I don't want to destroy them (funny how the word 'decimate' has changed definition over the years) :). We have a data acquisition program that saves its output to Excel's ..xls format....
2
by: Dhananjay | last post by:
Hi all , I have got problem when i am tring to exportGridview Data into Excel format. It is going into text format ,but what i want is if the field is number/currency then it should go into...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.