1

I'm looking to format the first column of my Export01.csv into dd/mm/yyyy. sadly the current format when I export it is dd/mm/yy mm:mm.

Could someone help me tweak my code to make the changes to the date during the import procedure if it's possible?

import sys
import cx_Oracle
import csv

connection = cx_Oracle.connect('user','password','ORPM2') 
cursor = connection.cursor()


SQL="SELECT * FROM EXPORT01"
cursor.execute(SQL)

filename="C:\Projects\SQL_Export\Export01.csv"

with open(filename,"wb") as fout:
writer = csv.writer(fout)
writer.writerow([ i[0] for i in cursor.description ]) # heading row
writer.writerows(cursor.fetchall())

fout.close()
cursor.close()
connection.close() 

I've added a sample of the first two columns of data in Export01

WEEK_ENDING         ORDER_HEADLINE
12/02/2016 00:00    Headline
15/01/2016 00:00    Headline
15/01/2016 00:00    Headline
1
  • You don't need to close your file 'fout' when you use "with" contextManager, also, you need to respect the indentation level. Commented Apr 15, 2016 at 14:18

1 Answer 1

1

If you handle the fetchall a row at a time, you could then convert the first column entry as follows:

from datetime import datetime
import sys
import cx_Oracle
import csv


connection = cx_Oracle.connect('user','password', 'ORPM2') 
cursor = connection.cursor()
SQL="SELECT * FROM EXPORT01"
cursor.execute(SQL)
filename = r"C:\Projects\SQL_Export\Export01.csv"

with open(filename, "wb") as fout:
    writer = csv.writer(fout)
    writer.writerow([i[0] for i in cursor.description ]) # heading row

    for row in cursor.fetchall():
        cols = list(row)
        cols[0] = cols[0].strftime("%d/%m/%Y")
        writer.writerow(cols)

cursor.close()
connection.close()

Also note, the with statement you are using will automatically close the file when you leave its scope. I would also recommend you prefix your file path with r to avoid Python trying to escape any of the backslashes in the path.

Sign up to request clarification or add additional context in comments.

9 Comments

I'm getting the following error Martin........ Traceback (most recent call last): File "C:/Users/803261400/desktop/Export2.py", line 25, in <module> row[0] = datetime.strptime(row[0], "%d/%m/%y %H:%M").strftime("%d/%m/%Y") AttributeError: 'module' object has no attribute 'strptime'
Sorry, forgot to copy over an import, you need to add from datetime import datetime
Martin, thanks for your help. I'm getting the same errors........Traceback (most recent call last): File "C:/Users/803261400/desktop/Export2.py", line 23, in <module> row[0] = datetime.strptime(row[0], "%d/%m/%y %H:%M").strftime("%d/%m/%Y") TypeError: must be string, not datetime.datetime >>>
Do you know which version of Python you are using?
I have assumed that the type of the first column is a string. Could you add print type(row[0]) and tell me what it says.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.