Hi all,

I'm working on my python script as I'm pulling the data from the sqlite3 database. I'm trying to convert the string object to datetime object, but I have got a problem with the code as I get the error when I'm trying convert from the string object to datetime object.

The error I'm getting is: TypeError: attribute of type 'NoneType' is not callable

The error are jumping on this line:

program_startdate = datetime.datetime.strptime(str(row[2]), "%Y%m%d%H%M%S")

Here is example the results from the sqlite3 database:

20140520170000
20140520170000 
20140520170000
20140520170000
20140520170000
20140520170000
20140520170000

Here is the code:

#get the channels list
cur.execute('SELECT channel FROM programs WHERE channel GROUP BY channel')

for row in cur:
    channels = row[0].encode('ascii')
    channelList.append(channels)


    # set the channels text
    for index in range(0, CHANNELS_PER_PAGE):
        channel = channelList[index]

        if channel is not None:
           self.getControl(4110 + index).setLabel(channel)


           #get the programs list
           cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel="channel"')
           programList = list()
           programs = cur.fetchall()

           for row in programs:
               program = row[1].encode('ascii'), str(row[2]), str(row[3])
               #print program
               #print datetime.datetime.strptime(str(row[2]), "%Y%m%d%H%M%S")

               program_startdate = datetime.datetime.strptime(str(row[2]), "%Y%m%d%H%M%S")
               #program_endDate = datetime.datetime.strptime(str(row[3]), "%Y%m%d%H%M%S")
               programList.append(program)


               # find nearest half hour
               viewStartDate = datetime.datetime.now()
               viewStartDate -= datetime.timedelta(minutes = viewStartDate.minute % 30, seconds = viewStartDate.second)

               #convert the datetime object between start and end date
               startDelta = program_startdate - viewStartDate
               #stopDelta = program_endDate - viewStartDate
               #print startDelta, stopDelta   # check if you're getting the result you want
               #cellStart = self._secondsToXposition(startDelta.seconds)
           cur.close()

I'm using python version 2.6.

Can you please help me how to fix the code to get rid of the error?

Thanks in advance

Recommended Answers

All 6 Replies

The docs are always a good place to start Click Here Strftime is part of the datetime.date class, not datetime.datetime.

Thank you for your help woooee. I can see it have fixed the problem, but now i have an error: TypeError: unsupported operand type(s) for -: 'str' and 'datetime.datetime'

The error are jumping on this line:

startDelta = program_startdate - viewStartDate

Here is the update code:

for row in programs:
    program = row[1].encode('ascii'), str(row[2]), str(row[3])
    program_startdate = str(row[2])
    program_endDate = str(row[3])

    # find nearest half hour
    viewStartDate = datetime.datetime.today()
    viewStartDate -= datetime.timedelta(minutes = viewStartDate.minute % 30, seconds = viewStartDate.second)

    #convert the datetime object between start and end date
    startDelta = program_startdate - viewStartDate
    stopDelta = program_endDate - viewStartDate

do you know how to fix this???

You can use the function type()
type(object) will give you the type of the specified object.

i don't know how i can use the function type to get the object. can you show me an example how i can do that?

Example ...

    n = 77
    print(type(n))  # <type 'int'>

    s = 'simple'
    print(type(s))  # <type 'str'>

    q = [1, 2, 3]
    print(type(q))  # <type 'list'>

    import time
    now = time.localtime()
    print(type(now))  # <type 'time.struct_time'>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.