0

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?

1 Answer 1

2

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.

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

1 Comment

Thanks @OrangeFlash81 - that did the trick. Now I need to somehow figure out how to set the timestamp to midnight...

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.