Load a broken csv file into duckdb

Some CSV files have issues with them. Below is a code snippet to save the information in a csv file into a duckdb database, ignoring all the ‘broken’ rows.

# pip install duckdb
import duckdb

# create context manager to connect to duckdb database
# a duckdb database will be created if there is not one already
# with that name / filepath
with duckdb.connect("database.duckdb") as con:

    # create table statement
    # rows with errors in will not be loaded into duckdb database
    make_table = '''
    CREATE TABLE raw_bronze AS
        SELECT 
            * 
        FROM read_csv('my_faulty.csv', ignore_errors=True);
    '''

    con.sql(make_table)
    
    # context manager will handle the closing of the connection
    # to duckdb database so no need to close it explicitly

Send a Comment

Your email address will not be published.