-1

I have a few parsers that collect data and make csv file, after collecting data I need to upload data from csv to my database(PostgreSQL)

p.s.table in database is already exist and just need to append data

How can I do this?

I have try to use sqlalchemy, but after connection don't know what to do

engine = create_engine('postgresql://postgres:username@localhost:5432/DB_name')

Didn't find information that could help me

1
  • 1
    You need to insert Commented Aug 17, 2022 at 9:28

1 Answer 1

0

Probably you are learning, because you didn't check the google before. I recommand to study the following:

https://docs.python.org/3/library/csv.html#csv.DictReader https://docs.sqlalchemy.org/en/14/tutorial/data_insert.html#tutorial-core-insert

So basically you have to do something like this:

from sqlalchemy import insert
import csv

with open('names.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        insert('user_table').values(name=row['name'], fullname=row['fullname'])

Then commit!

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.