Engine for LaTeX songbooks http://www.patacrep.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

30 lines
747 B

"""Dealing with encoding problems."""
import codecs
import chardet
import logging
import contextlib
LOGGER = logging.getLogger(__name__)
@contextlib.contextmanager
def open_read(filename, mode='r', encoding=None):
"""Open a file for reading, guessing the right encoding.
Return a fileobject, reading unicode strings.
If `encoding` is set, use it as the encoding (do not guess).
"""
if encoding is None:
with open(filename, 'rb') as file:
fileencoding = chardet.detect(file.read())['encoding']
else:
fileencoding = encoding
with codecs.open(
filename,
mode=mode,
encoding=fileencoding,
errors='replace',
) as fileobject:
yield fileobject