I have the following to extract data from a CSV into an SQLite db:
elif file.endswith('.csv'):
read_path = os.path.join(dir_read, file)
with open(read_path,'r',encoding='utf-8') as fin:
dr = csv.DictReader(fin)
to_db = [(i['InvoiceNumber'],i['InvoiceType'], i['ChargeType'],i['SupplierID'], i['Net_Amount']) for i in dr]
c.executemany("INSERT INTO mf (InvoiceNumber, InvoiceType, ChargeType, SupplierID, Net_Amount) VALUES (?, ?, ?, ?, ?) ;", to_db)
con.commit()
This works for most of the files I am importing (200 files), but there are a couple with a NUL in one of the fields, this is not one of the fields in the to_db
. I can see this in Notepad++, one row has a null all the others have an empty string.
The error I get is:
_csv.Error: line contains NULL byte
I have tried different encoding (utf-16, le, be), but then they fail on all the other csv files.
Is there a way to remove these NULL
values and replace with a blank string when importing?