1
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 796: ordinal not in range(128)

My goal is get the text from a text file and save it as a variable in my program. I do not know how it is encoded. If someone could tell me how to find out how it is encoded, that may also be helpful

Nick Rizzolo
  • 125
  • 1
  • 2
  • 12
  • 3
    I understand that Python's Unicode handling can be a bit confusing; could you please show us the code that you are using, and wich version of Python, so that we could take a better look? – Haroldo_OK Jul 29 '17 at 21:02
  • This other post could help you partially: https://stackoverflow.com/questions/147741/character-reading-from-file-in-python – Haroldo_OK Jul 29 '17 at 21:06
  • Okay, I found something closer to what you want: https://stackoverflow.com/questions/436220/determine-the-encoding-of-text-in-python – Haroldo_OK Jul 29 '17 at 21:09
  • If the first character where it balked was a 0xc2 it's pretty likely that the original text is in UTF-8. – Matteo Italia Jul 29 '17 at 21:12
  • import codecs x = codecs.open('/Users/name/Desktop/CommercialArticles/3_Biotech/3_Biotech#1.txt'',encoding='utf8' 'r'); file_contents = x.read() print (file_contents) – Nick Rizzolo Jul 29 '17 at 21:55

0 Answers0