By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

CSV help required

P: 2
= 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 ':'
  2.  
  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

Simon
Aug 2 '07 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
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.

Thanks.
Aug 2 '07 #2

Post your reply

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