Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python3 psycopg2查询前测试时间戳值_Python 3.x_String_Postgresql_Psycopg2 - Fatal编程技术网

Python 3.x Python3 psycopg2查询前测试时间戳值

Python 3.x Python3 psycopg2查询前测试时间戳值,python-3.x,string,postgresql,psycopg2,Python 3.x,String,Postgresql,Psycopg2,使用类似于2020-10-26T14:33:00的Python字符串值,在执行psycopg2查询以插入数据库之前,是否有方法测试Postgresql是否会接受它?在Python中处理日期(时间)时,我发现该模块非常宝贵。使用它的parse部分将捕获大多数格式错误的时间戳字符串,并将那些不正确的字符串转换为psycopg2可以使用的日期时间: from dateutil.parser import parse parse('33/9/2020') ParserError: day is out

使用类似于
2020-10-26T14:33:00的Python字符串值,在执行psycopg2查询以插入数据库之前,是否有方法测试Postgresql是否会接受它?

在Python中处理日期(时间)时,我发现该模块非常宝贵。使用它的
parse
部分将捕获大多数格式错误的时间戳字符串,并将那些不正确的字符串转换为
psycopg2
可以使用的日期时间:

from dateutil.parser import parse
parse('33/9/2020')
ParserError: day is out of range for month: 33/9/2020

parse('26/10/2020 14:33')
Out[4]: datetime.datetime(2020, 10, 26, 14, 33)

import psycopg2
con = psycopg2.connect("dbname=test host=localhost user=postgres")
cur = con.cursor()
cur.execute("insert into dt_test values(%s, %s)", (8, parse('26/10/2020 14:33')))
con.commit()

select * from dt_test;

 id |       ts_fld        
----+---------------------
  8 | 10/26/2020 14:33:00


你说接受是什么意思?可能就是你要找的?。。。通常,表中的列和python类型应该是,然后psycopg2关心它。在尝试将字符串作为postgresql tiemstamp插入之前,我需要验证时间戳。如果我使用26/10/2020 14:33,我有以下错误:错误:日期/时间字段值超出范围:“26/10/2020 14:33”,提示:可能需要不同的“datestyle”设置。您可能需要将字符串转换为datetime对象,请查看文档: