Home > Software design >  How to add different type of files in postgresql on Python
How to add different type of files in postgresql on Python

Time:11-25

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?

CodePudding user response:

  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)

  • Related