0
votes

This is my script to save the output into a csv file:

import os, csv, datefinder, datetime
os.chdir('C:\Users\dul\Desktop\Article')

with open("test2.txt", 'r') as file1:
  text1 = file1.read()

matches = list(datefinder.find_dates(text1))

if len(matches) > 0:
    date = matches[1]
    print date
else:
    print 'No dates found'

csv = open(date, "w")

columnTitleRow = "date, time\n"
csv.write(columnTitleRow)

When I run this script, I get this error message:

[Traceback (most recent call last): File "C:\Users\dul\Desktop\Article\ap.py", line 18, in csv = open(date, "w") TypeError: coercing to Unicode: need string or buffer, datetime.datetime found]

1
Next time please add the full error stacktrace to your question. That helps us to see in which line the error happens and which underlying function is raising the error. - Ralf
@Ralf Ok. Thanks for letting me know! - Philip

1 Answers

1
votes

You cannot pass a datetime instance to open(); that function needs a string that indicates the filename of the file it should open.

Are you sure that you need to open a file that has the name of the date? If that really is you case, then you need to at least cast the datetime object to a str:

csv = open(str(date), "w")

But I doubt that that really is what you need.