How to Ignore Null Byte When Reading a CSV File

how to skip / ignore skip null byte in csv file using pd.read_csv?

Try to save the csv file as UTF-16 then try to run code:

pd.read_csv(fname, header=None, na_values='-32768', names=binnams, engine='python')

Python CSV error: line contains NULL byte, but no NULL byte found in the file

I found what the issue was. I was reading the files from an external hard drive formatted in NFTS, while the code was running on a macOS formatted in HFS.

After formatting the external drive to match the formatting on my laptop, the problem of null bytes disappeared.

Line contains NULL byte in CSV reader (Python)

I've solved a similar problem with an easier solution:

import codecs
csvReader = csv.reader(codecs.open('file.csv', 'rU', 'utf-16'))

The key was using the codecs module to open the file with the UTF-16 encoding, there are a lot more of encodings, check the documentation.

list contains NULL byte, CSV DictReader

You can replace your NULL bytes by an empty string. Like this:

 reader = csv.DictReader(x.replace('\0', '') for x in file)

Example:

with open('excelfile.csv', 'r', encoding="ISO-8859-1") as file:

reader = csv.DictReader(x.replace('\0', '') for x in file)
for row in reader:
frame = {'bank': row['BANK'], 'ifsc': row['IFSC'], 'branch': row['BRANCH'], 'address': row['ADDRESS'] }
framelist.append(frame)


Related Topics



Leave a reply



Submit