将python列表插入PostgreSQL数据库

2024-04-27 09:29:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个剪贴项目,我正在努力把数据插入数据库。

我的数据库有三列:id、post和author 刮削的数据存储在两个列表中。第一个是列表文章,第二个是作者。两者的长度总是相同的,因为没有没有内容的文章或没有作者的文章。

我尝试了this问题中提供的解决方案,但出现以下错误:

Traceback (most recent call last):
File "scrape.py", line 69, in <module>
cur.execute("INSERT into posts(id, post, author) VALUES (%s, %s, %s)", authors)
TypeError: not all arguments converted during string formatting

我的列表如下:

author = [n1, n2, n3 ... n45]
posts = [n1, n2, n3 ... n45]

我想把它们像这样插入数据库:

ID | Post | Author
---|------|--------
1  | Text | Author
2  | Text | Author

稍后,我需要从post列的值中提取统计信息。确切地说是多少次出现,但我想这符合另一个问题。


Tags: 数据textid数据库列表文章作者post