zubmic
zubmic

Reputation: 77

Insert python list into PostgreSQL database

I'm working on a scraping project and I'm struggling with inserting data into database.

My database has three columns: id, post and author Scraped data is stored in two lists. First one is list posts and second one is authors. Both are the same length always because there are no articles without content or articles without author.

I've tried solution provided in this question but I got this error:

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

My lists look like this:

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

And I would like to insert them into database like this:

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

Later on I would need to extract stats from values in post column. To be exact word and number of how many times they appear but I guess it qualifies for another question.

Upvotes: 2

Views: 13871

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

You tried to insert a list of authors into a table of posts ? That doesn't quite make sense.

Surely you're looking for something like this

data = [(1, 'post', 'author'), (2, 'post', 'author')]
for d in data:
    cur.execute("INSERT into posts(id, post, author) VALUES (%s, %s, %s)", d)

Also id values are typically integers, but you seem to be formatting as a string

If you want to keep individual lists, check out the result of zip(posts, authors)

Upvotes: 6

Related Questions