Reputation: 102
So I have a script which uses the following imports:
import sys
import os.path
import requests
import json
import csv
import glob2
import shutil
from time import sleep
from time import gmtime, strftime
import time
from datetime import datetime
I'm trying to convert row[15]
which is a date string that looks like "23/03/2015 00:00"
(read from a csv) into unix time.
This is what that portion of code looks like:
{"name":"referral_date", "value": time.mktime(datetime.strptime(row[15], "%d/%m/%Y %H:%S").timetuple()) }
Every time I try it, it comes back with this:
AttributeError: 'str' object has no attribute 'mktime'
And yet, in a seperate test file, i've tried the following:
import time
from datetime import datetime
mylist = ["23/03/2015 00:00"]
test = time.mktime(datetime.strptime(mylist[0], "%d/%m/%Y %H:%S").timetuple())
print(test)
Which, as you'd expect, results in 1427031000.0
- what am I doing wrong?
Upvotes: 0
Views: 768
Reputation: 11837
Look over your code: somewhere you've probably declared a variable called time
which is a string. It's trying to call mktime
on that variable, which is why your error refers to a str
object.
The easiest way to resolve this is to change the name of the time
variable to something else. If you wanted to fix the issue without changing that variable name, you could import time
with a different name like this:
import time as timelibrary
And then call timelibrary.mktime
.
Upvotes: 2