4
votes

Is there any way to preprocess text files and skip these characters?

UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 1395: invalid start byte
2
can you show what you written??? - Hackaholic
Sure there is, but do you want to? Wouldn't it be better to use the proper codec for decoding in the first place so it all comes back intact? - Mark Ransom
@MarkRansom I guess the better question is then, how do you find the proper codec given a text file? - Maximus S
@MaximusS since you never told us where the data came from, we can't answer that question. Maybe you can? - Mark Ransom

2 Answers

18
votes

Try this:

str.decode('utf-8',errors='ignore')
4
votes

I think your text file have some special character, so 'utf-8' can't decode.

You need to try using 'ISO-8859-1' instead of 'utf-8'. like this:

   import sys
   reload(sys).setdefaultencoding("ISO-8859-1")

   # put your code here