0

I have the below code

stream = io.StringIO(csv_file.stream.read().decode('utf-8-sig'), newline=None) // error is here

reader = csv.DictReader(stream)

list_of_entity = []
line_no, prev_len = 1, 0,

for line in reader:

While executing the above code I got the below error.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 252862: invalid start byte

Later to fix this I tried the below.

stream = io.StringIO(csv_file.stream.read().decode('unicode_escape'), newline=None)

reader = csv.DictReader(stream)

list_of_entity = []
line_no, prev_len = 1, 0,

for line in reader:// error is here

when i change decode as unicode_escape it thrown the error "

_csv.Error: line contains NULL byte" at above highlighted comment line.

There is null byte present in csv, I want to ignore or replace it. can anyone help on this.

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • 1
    It's likely your data is a `bytestream`, use `.BytesIO` instead. – stovfl May 15 '19 at 06:34
  • @stovfl how to use .BytesIO in my code, I have tried replacing stream = io.BytesIO(csv_file.stream.read().decode('utf-8')) but its error saying "TypeError: a bytes-like object is required, not 'str'" I think this is not the correct way of using it. can you help me here how can i use BytesIO in my code. – Eshwin Sukhdeve May 15 '19 at 06:56
  • [Edit] your Question and explain or show **why** do you use `csv_file.stream.read()`? Variable `csv_file` is unknown, which type is it? Do you simple want to read a `CSV` file from disk? – stovfl May 15 '19 at 12:02

0 Answers0