Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 g Null Byte using DictReader
#1
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.
Quote
#2
Have you tried plain utf-8 ?
Quote
#3
Hi Larz60+
I tried with utf-8 also but still same error.

_csv.Error: line contains NULL byte
Quote
#4
(May-15-2019, 05:08 AM)eshwinsukhdeve Wrote: I want to ignore or replace it
pass errors argument with value 'ignore' or 'replace' to decode()

the docs
Quote
#5
can you tell me in code how to pass it error argument'ignore' or 'replace' here
Quote
#6
stream = io.StringIO(csv_file.stream.read().decode('utf-8-sig', error='ignore'), newline=None)

and you can also try chardet - https://pypi.org/project/chardet/
for help on what the encoding is
Quote
#7
it says,

TypeError: 'error' is an invalid keyword argument for this function
Quote
#8
sorry, it's errors, not error - my bad
stream = io.StringIO(csv_file.stream.read().decode('utf-8-sig', errors='ignore'), newline=None)
Quote
#9
Still getting same error.
csv.Error: line contains NULL byte

the main issue is in below line of code
for line in reader:
reader object contains null bytes and throwing error here
Quote
#10
(May-15-2019, 05:08 AM)eshwinsukhdeve Wrote: stream = io.StringIO(csv_file.stream.read().decode('utf-8-sig'), newline=None) // error is here
(May-15-2019, 05:08 AM)eshwinsukhdeve Wrote: 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

My suggestion should fix this error produced by this code
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 7 283 Sep-12-2019, 11:19 PM
Last Post: newbieAuggie2019
  Handling null or empty entries from Entry Widget KevinBrown 1 251 Mar-17-2019, 04:22 PM
Last Post: perfringo
  Array/Jarray problem contains null value? Maverick0013 1 799 Aug-10-2018, 12:56 PM
Last Post: Windspar
  Replace null values in Json file burnsssss 11 7,532 Jun-11-2018, 09:45 AM
Last Post: burnsssss
  4 byte hex byte swap from binary file medievil 7 6,138 May-08-2018, 08:16 AM
Last Post: killerrex
  csv.reader vs csv.dictReader mepyyeti 3 6,017 Mar-20-2018, 08:31 AM
Last Post: buran
  xmlrpc.client, ServerProxy and null return thepurpleblob 0 1,057 Oct-26-2017, 10:26 AM
Last Post: thepurpleblob

Forum Jump:


Users browsing this thread: 1 Guest(s)