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
JavaScript
x
2
1
engine = create_engine('postgresql://postgres:username@localhost:5432/DB_name')
2
Didn’t find information that could help me
Advertisement
Answer
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:
JavaScript
1
8
1
from sqlalchemy import insert
2
import csv
3
4
with open('names.csv', newline='') as csvfile:
5
reader = csv.DictReader(csvfile)
6
for row in reader:
7
insert('user_table').values(name=row['name'], fullname=row['fullname'])
8
Then commit!
Good luck!