Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError: 'charmap' codec can't encode character / character maps to <undefined> #55

Closed
bitsgalore opened this issue May 9, 2017 · 4 comments
Labels

Comments

@bitsgalore
Copy link
Member

bitsgalore commented May 9, 2017

While processing Sranan Rutu DVD (PPN=344588807), following error to stdout:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Python36\lib\logging\__init__.py", line 994, in emit
    stream.write(msg)
  File "C:\Python36\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u0301' in position
93: character maps to <undefined>
Call stack:
  File "C:\Python36\lib\threading.py", line 884, in _bootstrap
    self._bootstrap_inner()
  File "C:\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "f:\johan\pythonCode\iromlab\iromlab\cdworker.py", line 508, in cdWorker
    success = processDisc(carrierData)
  File "f:\johan\pythonCode\iromlab\iromlab\cdworker.py", line 107, in processDi
sc
    logging.info(''.join(['Title: ',carrierData['title']]))
Message: "Title: Sranan Rutu : duizenden pagina's naslagmateriaal op één schij
fje, uitgegeven ter gelegenheid van het 10-jarig bestaan van de Stichting voor S
urinaamse Genealogie, 2001-2011 / Stichting voor Surinaamse Genealogie"
Arguments: ()

Error seems to happen in logger.

@bitsgalore bitsgalore added the bug label May 9, 2017
@bitsgalore
Copy link
Member Author

Same error, different disc:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Python36\lib\logging\__init__.py", line 994, in emit
    stream.write(msg)
  File "C:\Python36\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u0300' in position
186: character maps to <undefined>
Call stack:
  File "C:\Python36\lib\threading.py", line 884, in _bootstrap
    self._bootstrap_inner()
  File "C:\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "f:\johan\pythonCode\iromlab\iromlab\cdworker.py", line 508, in cdWorker
    success = processDisc(carrierData)
  File "f:\johan\pythonCode\iromlab\iromlab\cdworker.py", line 107, in processDi
sc
    logging.info(''.join(['Title: ',carrierData['title']]))
Message: 'Title: The audio collection. III : more than 75 audio designs for home
 construction = mehr als 75 Audio-Selbstbauschaltungen = plus de 75 projets audi
o à réaliser soi-même = meer dan 75 audio-ontwerpen voor zelfbouw'
Arguments: ()

@bitsgalore
Copy link
Member Author

Minimal script that replicates the issue:

https://gist.github.com/bitsgalore/a7b18aaf08ecbdb3391d5a9fa8d1ce9a

Apparently the error arises when writing strings with non-ASCII UTF-8 chars to the log file. Writing to the GUI window works OK.

Solution would be to add encoding parameter to the handler:

http://stackoverflow.com/questions/10706547/add-encoding-parameter-to-logging-basicconfig

@bitsgalore
Copy link
Member Author

This seems to work, old code:

    logging.basicConfig(filename='test.log',
        level=logging.INFO, 
        format='%(asctime)s - %(levelname)s - %(message)s')

Replace by this:

    logging.basicConfig(handlers=[logging.FileHandler('test.log', 'w', 'utf-8')], 
                level=logging.INFO,
                format='%(asctime)s - %(levelname)s - %(message)s')

@bitsgalore
Copy link
Member Author

Fixed: b2b7569

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant