How to add different type of files in postgresql on Python


I need to add different types of files (CSV, XML, xlsx, etc.) to the database (Postgresql). I know how I can read it via pandas, but I have some issues with adding this to the database.

What libraries do I need to use? And does it need to convert them into one format?

Asked By: Tomas Adomson



  1. Read files with pandas:

    csv_df = pd.read_csv(‘file.csv’)

    xml_df = pd.read_xml(‘file.xml’)

    xlsx_df = pd.read_excel(‘file.xlsx’)

  2. Add tables in db with columns like in your file

  3. Add files to db

    xlsx_df.to_sql(‘table_name’, engine, if_exists=’replace’, index=False)

Answered By: Nero
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.