The default encoding for Python 3.5 files is "utf-8."
By default, file encoding for Windows tends to be something else.
If you are going to open two text files, you can try the following:
import locale locale.getdefaultlocale() file1 = input("Enter the name of the first file: ") file1_open = open(file1, encoding=locale.getdefaultlocale()[1]) file1_content = file1_open.read()
Auto-discovery should be detected in the standard library.
Otherwise, you can create your own:
def guess_encoding(csv_file): """guess the encoding of the given file""" import io import locale with io.open(csv_file, "rb") as f: data = f.read(5) if data.startswith(b"\xEF\xBB\xBF"):
and then
file1 = input("Enter the name of the first file: ") file1_open = open(file1, encoding=guess_encoding(file1)) file1_content = file1_open.read()
source share