I was kind of struggling to get python into reading this particular text file.. (fig 1)
I tried some encoding (utf-8, ascii..) But none worked. Then after a while I found the solution in the traceback. (fig 2)
Now my question is how does this result in an error when python is reading the right encoding?
Figure 1:
rel_path = "DIR/text.txt"
print ('Getting data from: ' + rel_path + ': \n')
text_file = open(rel_path)
print (text_file.read())
Figure 2:
File "test.py", line 14, in <module>
print (text_file.read())
File "LOCALDIR\Python\Python35\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2018' in position 4590:
character maps to <undefined>
Note the file python reads PYTHONDIR\cp850.py <-
When I add encoding='cp850' when opening the text file it works. (fig 3)
Figure 3:
rel_path = "DIR/text.txt"
print ('Getting data from: ' + rel_path + ': \n')
text_file = open(rel_path, encoding='cp850')
print (text_file.read())