I want to enter data of type datetime in the python array.

Asked 2 years ago, Updated 2 years ago, 39 views

Sorry for the unclear question

When I try to enter data of datetime type stored in psql in python array, the first one goes in, but the second one is told that the type is different, so I can't enter it anymore

Here's the source.

#-*-coding:utf-8-*- 
# -*-coding:unicode -*- 
import csv 
import binascii 
import string 
import psycopg2 
import datetime 
from datetime import datetime as dt 

connection=psycopg2.connect("(omitted)") 
connection.get_backend_pid() 

cur=connection.cursor() 
# SQL command execution (this time table creation) 
try: 
    cur.execute("CREATE TABLE table(id serial, date date, time time, idm text, deviceid);") 
    print 'File Created' 
except Exception as IOError: 
    print 'File Found' 
connection.commit() 

i = 0 
cur.execute("select * from table") 
# date entry 
now = 0 
count = 0 
For row in cur: 
if not now==row[1]: 
print now, row[1], count 
date=(row[1])#Create Array
For lin range (count): 
    try: 
        print row[1], date[l], l  
    except Exception as: 
        break 
    if now!=0 and row[1]==date[l]: 
        break 
    count = count +1 
    count_max = count 
    date=date+row[1]#Add to array (where error occurs)  
    now=row[1] 
    active[row[3], row[1]] = 1 

error messages:

Traceback (most recent call last): 
File "actable.py", line 43, in <module> 
date=date+row[1] 
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'datetime.date' 

I don't know the solution at all because it should be the same type even if they say it's different

python

2022-09-29 21:45

1 Answers

I can't read the intent of the code, but I guess it's

date=(row[1])

What I wanted to do in the section is

date=[row[1]]

Wasn't it?

(row[1]) parentheses are considered to be parentheses for changing the priority of operations, so they are redundant and meaningless.

And

date=date+row[1]

also

date=date+[row[1]]

Or

date.append(row[1])#This is better

That's right.


2022-09-29 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.