Change the comma delimiter in python output to csv

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaf3773
    New Member
    • Jan 2012
    • 20

    Change the comma delimiter in python output to csv

    I am trying to output a list to a csv file and the line below work fine.

    I need help with changing the delimiter from the default comma(,) to tab delimited.
    csv_file = open('output.cs v', 'w')
    csv_writer = csv.writer(csv_ file)

    Help very much appreciated

    Kaf
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    This is done by setting a dialect in your csv writer. Dialect is an csv object and you can set the dialect.delimit er to '/t'.

    See here

    Comment

    • kaf3773
      New Member
      • Jan 2012
      • 20

      #3
      Thanks for the pointer Glenton. I got it to work by doing this
      csv_writer = csv.writer(csv_ file, delimiter="\t")

      I am trying to use a date in an SQL where clause

      prevday = datetime.date.t oday()-datetime.timede lta(1)
      prev_day = prevday.strftim e("%d-%b-%y")

      and this is the SQL statement but it does not work

      cur.execute(""" select switch,percent_ occupied from Table_name where switch =
      ('4DYHTE') and trunc(date_time ) = %s""",("prev_da y",))

      I will appreciate any help

      Thanks
      Kaf

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Can you let us know what you mean by "does not work"? What's the error message? Does the prev_day need quotation marks?

        Comment

        • kaf3773
          New Member
          • Jan 2012
          • 20

          #5
          Well i am trying to pass the data in the variable prev_day which is in this format dd-mon-yy to the SQL query.

          without the quotes i get an invalid variable error

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            prevday = datetime.date.t oday()-datetime.timede lta(1)
            Print prevday to see what it contains. I don't think there is a default for timedelta so you have to specify one whatever, days=1, weeks=1, etc. From the docs
            class datetime.timede lta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

            Comment

            • kaf3773
              New Member
              • Jan 2012
              • 20

              #7
              Hello Glenton,

              Sorry about this but i made a mistake about the error i was getting.

              When i run it with or without the quotes this is the error message i get
              Traceback (most recent call last):
              File "spit.py", line 9, in <module>
              ('4DYHTE') and trunc(date_time ) = %s""",("prev_da y",))
              cx_Oracle.Datab aseError: ORA-01036: illegal variable name/number

              dwblas,

              Concerning what is stored in prevday its the date for the previous day
              >>> import datetime
              >>> prevday = datetime.date.t oday()-datetime.timede lta(1)
              >>> print prevday
              2012-01-04
              >>>
              and i format it with this prev_day = prevday.strftim e("%d-%b-%y")
              to get
              >>> prev_day = prevday.strftim e("%d-%b-%y")
              >>> print prev_day
              04-Jan-12
              >>>
              which i want to pass to the SQL statement for the query.

              Thanks for your help

              Comment

              • kaf3773
                New Member
                • Jan 2012
                • 20

                #8
                i finally figured it out by doing this


                cur.execute(""" select switch,percent_ occupied from Table_name where switch = '4DYHTE' and trunc(date_time ) = ('%s')""" % (prev_day))

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Congratulations on solving your problem. Thanks for the feedback!

                  Comment

                  Working...