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

2
  • try s=["'2017/01/30", "blah"] (add a leading quote) Commented Jan 31, 2017 at 21:07
  • that fixed it! but I don't want a quote there.. Commented Jan 31, 2017 at 21:09

2 Answers 2

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.

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

Comments

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']]

1 Comment

Thank you sir! When opened with notepad, I could verify I was writing it correctly

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.