Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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 类型错误:';datetime.timedelta';对象不可移植BaseHttpServer问题_Python_Postgresql_Basehttpserver - Fatal编程技术网

Python 类型错误:';datetime.timedelta';对象不可移植BaseHttpServer问题

Python 类型错误:';datetime.timedelta';对象不可移植BaseHttpServer问题,python,postgresql,basehttpserver,Python,Postgresql,Basehttpserver,我使用BaseHttpServer建立了一个基本的python Web服务器,并正在练习从postgresql数据库查询数据。一切进展顺利,但在解析SQL结果时出现了一个错误。 这是我的密码: cur=con.cursor() cur.execute(('select * from stop_times as a, stop_times as b where a.train_id=b.train_id and a.station_name = %s and b.station_name= %s

我使用BaseHttpServer建立了一个基本的python Web服务器,并正在练习从postgresql数据库查询数据。一切进展顺利,但在解析SQL结果时出现了一个错误。 这是我的密码:

cur=con.cursor()
cur.execute(('select * from stop_times as a, stop_times as b where a.train_id=b.train_id and a.station_name = %s and b.station_name= %s and a.arrival_time < b.arrival_time'), (origin_name, dest_name))

self.wfile.write("Train Number &nbsp &nbsp  Starting Station &nbsp &nbsp  Destination Station &nbsp &nbsp Departure Time &nbsp &nbsp Arrival Time <br />")

while True:
    row=cur.fetchone()
    if row==None:
        break

    print row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]

    for item in row[0]:
        self.wfile.write("%s"% item)
    self.wfile.write(" &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp")
    for item in row[3]:
        self.wfile.write("%s"% item)
    self.wfile.write(" &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp")

    for item in row[8]:                         
        self.wfile.write("%s"% item)
    self.wfile.write("&nbsp &nbsp &nbsp &nbsp")

    for item in row[2]:
        self.wfile.write("%s"% item)
    self.wfile.write("&nbsp &nbsp")
我只是尝试将某些列输出到网页,当我到达第[2]行的最后一个for循环时,我得到以下错误:

File "./caltrainServer.py", line 129, in performQuery
for item in row[2]:
TypeError: 'datetime.timedelta' object is not iterable

我检查了,数据库中的这些列的类型为interval。如何像对其他类型为varchar的列那样迭代它们?

您在不必要地迭代行值的内容。像这样的台词:

for item in row[0]:
    self.wfile.write("%s"% item)
可能会改为

self.wlfile.write(row[0])
row[x]
是一个字符串时,您所做的实际上是在每个字母上迭代并写入它。但是
行[2]
是一个datetime.datetime对象。由于datetime.datetime对象是不可编辑的,因此您会收到该错误消息

尝试以下方法:

self.wlfile.write((row[2]))

将datetime对象放置在可以迭代的元组中。

行[2]
是一个“datetime.timedelta”,您无法从其中获取“item”(因为它只有自身)。我认为您不需要进行任何迭代,您可以只
self.wfile.write(“{0},{1}…{9}”).format(行[0],行[1]…行[9])
?您打算使用html实体
(不是
)?啊,是的,我应该键入GordonsBeard:这是输出格式的更好的方法,谢谢。
self.wlfile.write((row[2]))