0

I am consistently running into errors when opening a file in my program. The original code was a simple call to open, f = open(file_name, 'w') or with open(file_name) as f:. Both of these threw similar errors of the sort UnicodeEncodeError: 'charmap' codec can't encode characters: character maps to <undefined>.

Answers to similar questions were to specify encoding='utf-8' inside the open function, which I tried and it still resulted in an error. Another answer to the same question was to set encoding='Latin1' but this was advised against because -- from what I understand -- Latin1 will work with any file type regardless of whether or not it is actually encoding it correctly or not. If this is misinformation please do let me know because I tried it using Latin1 regardless and it ran without error.

I even tried to download an editor which tells you the method of encoding preferred for the file that you're using. I gave it one of the .h5 files that I am trying to open in the program and it said that I should be using Hexadecimal, which I have only been able to find support for through the codecs module when using the encoder function. via Sublime Text

Above makes me think that using Latin1 may be incorrect practice. Therefore my question is:

Is there some way to specify Hexadecimal to the encoding parameter of python's open function? If so, how would that be done? (I should note that I have tried the obvious solutions encoding='hex', encoding='hexadecimal' and both were unrecognized) And/or is using Latin1 incorrect practice for the .h5 files that I am trying to open?

  • An encoding is a way of interpreting a series of bytes, which are just integer values. "Hexadecimal" is just one format for *displaying* an integer value. – chepner Jun 21 '19 at 17:02
  • So, what would be the correct value to give to the encoder parameter in my case? I'm fairly sure that giving it some value other than its default is what is assuaging my error. – Maria Morejon Jun 21 '19 at 17:10
  • The correct value is whatever matches the actual encoding of bytes in your file. – stark Jun 21 '19 at 17:13
  • 1
    If you are trying to read a binary file see https://stackoverflow.com/questions/1035340/reading-binary-file-and-looping-over-each-byte – stark Jun 21 '19 at 17:14

0 Answers0