CSV help required

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simondaviddrew
    New Member
    • Jul 2007
    • 2

    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!)


    Code:
    linessplit=[s1.split(':',1) for s1 in lines]    # Form Labels end with ':'
    
    if(some_condition):
      logfile.write(today)
      logfile.write('\t')
      splitvalues=["'"+x[1].strip() for x in linessplit]   
      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
    Code:
    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
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    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.

    Comment

    Working...