How do I remove NUL characters from a CSV using Python

lsward picture lsward · Aug 4, 2016 · Viewed 10.3k times · Source

I've noticed that several CSVs that come my way have random 'NUL' values placed through out the file. I noticed this because when I import the file into a database using a SSIS package I built those files throw a no column delimiter found error. I'm thinking about writing a python script to clean these files up, but I can't find a solution to this problem in Python. How would I use Python to remove these NUL characters?

I would include a picture, but I don't have enough reputation to include one.

Ex. "123456","Brown, Jim","","?NUL","",False,"8/16/2014 12:00:00 AM",""NUL,""InboNULund"

Answer

lsward picture lsward · Aug 15, 2016

I don't know how I didn't find this answer in my search, but this solution worked. It's weird that it worked because I tried the string replace method and it didn't seem to catch all of them but I think the through answer provided by @JohnMachin in this post really laid the ground work for me solving the problem. He provides a comprehensive way to investigate the problem and I suggest taking a look at it if you are having a similar issue. Python CSV error: line contains NULL byte