Writing into a csv file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michelle Lermon
    New Member
    • Aug 2010
    • 7

    Writing into a csv file

    I am trying to write some data into a csv file, but when I write the file the data skips a line. I tried putting commas at the end of the write line but that did not work.

    Attached is the part of the code where I write the data.

    Code:
    writer.writerow(datim + [button1]+ [row['cruise']] + [row['crunum']]+ [sitecode]+ [SeriesCode]+ [deploynum]+ [DeploymentCode]+ [ctd]+ [blat] + [blon]+[deploymenteklog]+ [Deploymentcom1 + str(deploynum)]),
    
    writer.writerow(datim + [button2]+ [row['cruise']] + [row['crunum']]+[sitecode]+[SeriesCode]+[deploynum]+[DeploymentCode]+[ctd]+[elat]+[elon]+[deploymenteklog]+[Deploymentcom2 + str(deploynum)]),
    Thanks
    Last edited by Niheel; Aug 5 '10, 02:41 PM. Reason: please use codetags to display code [code] . . . [/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    There's not much to go on, because I have no idea what the variables are assigned to. My guess is that one of the values has a trailing newline character. The newline character can be removed with str method strip().

    Comment

    • Haiyan
      New Member
      • Jul 2010
      • 17

      #3
      Since you are using the csv module and the default line termination sequence is '\r\n', that's why you get a new line. You can try following way and it should work for you.
      Code:
      writer.writerow(yourrow, lineterminator = '\r')
      Last edited by bvdet; Aug 7 '10, 01:02 AM. Reason: Add code tags

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        You can also use strip(), and avoid the possibility that some lines contain a newline and some do not.
        Code:
        str_out = ""
        for field in [datim, button1, row['cruise'], row['crunum'], sitecode, SeriesCode, deploynum, DeploymentCode, ctd, blat, blon, deploymenteklog, Deploymentcom1,  str(deploynum)]:
            str_out += field.strip()
        And then write str_out configured however you wish.

        Comment

        Working...