2
import csv

s=["2017/01/30", "blah"]
CSVreport = open("test.csv", "wb")
wr = csv.writer(CSVreport)
wr.writerow(s)

CSVreport.close()

Hi, I am trying to add date that is in type String to CSV. Above is my attempt but in my test.csv, date looks like: 30/01/2017

Please help

Thanks

Taewan Kang
  • 35
  • 1
  • 4

2 Answers2

2

Please check this thread to convert string to data format. Converting string into datetime

I tried your steps on my machine and I could print the string exactly as "2017/01/30". So it has to be some setting in your machine, did you open the csv in excel. Try opening it in text editor like notepad or vi as Excel may manipulate the date format.

Community
  • 1
  • 1
1

You are actually writing the date correctly in the csv file. If you open the csv file with a text editor, you will see the data is written exactly as you want.

To verify that, you can read data from the file.

import csv

s=["2017/01/30", "blah"]
CSVreport = open("test.csv", "wb")
wr = csv.writer(CSVreport)
wr.writerow(s)
CSVreport.close()

with open("test.csv", "rb") as f:
    reader = csv.reader(f)
    your_list = list(reader)
    print(your_list) # it prints --> [['2017/01/30', 'blah']]
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161