469,592 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

CSV help required

= OS: XP
= Python v. 2.5

I'm fairly new to all this, so be gentle... ;-)
I've written a small script which strips values from a form and writes them to a CSV (excel-tab). Some of the data I expect to import is in the form of large numbers, which Excel habitually displays as Scientific Notation (not very helpful!)

Expand|Select|Wrap|Line Numbers
  1. linessplit=[s1.split(':',1) for s1 in lines]    # Form Labels end with ':'
  3. if(some_condition):
  4.   logfile.write(today)
  5.   logfile.write('\t')
  6.   splitvalues=["'"+x[1].strip() for x in linessplit]   
  7.   logfile.write('\t'.join(["%s" % (v,) for v in splitvalues]))
This works, but all the data is prepacked in single quotes. Not elegant.

I tried
Expand|Select|Wrap|Line Numbers
  1. splitvalues=[str(x[1].strip()) for x in linessplit] 
but Excel doesn't appear to recognise the explicit string conversion. Am I missing something?

The ideal solution would be if users could switch off Scientific Notation display behaviour as a default when starting Excel. (I've looked, but can't see where to do this.)

The next-best thing would be if there were a CSV dialect (excel-nosci) which does the appropriate thing. I don't thing setting QUOTING = ALL *is* appropriate, but I'm willing to be persuaded!

Any help would be warmly welcomed

Aug 2 '07 #1
1 1088
6,596 Expert 4TB
Reading from a file alway yields strings. If that were not the case, .strip() wouldn't work. So there's no need to use str(). I think that the main problem lies within Excel: cell formatting, etc. If Excel has templates, I'd set one up to be the receptacle of your data. When you get it working by hand, Python can even open Excel, load the values and save the file for you.

Tip: Use lots of print statements so that you can see what is going on inside your script.

Also, supply a sample of your data so that we can play around with it.

Aug 2 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Gregory (Grisha) Trubetskoy | last post: by
5 posts views Thread by Lorenzo Bolognini | last post: by
6 posts views Thread by yzzzzz | last post: by
16 posts views Thread by Georges Heinesch | last post: by
2 posts views Thread by bufbec1 | last post: by
3 posts views Thread by Orchid | last post: by
11 posts views Thread by Naeem | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.