IronLanguages / ironpython3

Implementation of Python 3.x for .NET Framework that is built on top of the Dynamic Language Runtime.
Apache License 2.0
2.48k stars 287 forks source link

Lookuperror: unknown encoding: cp949 #1768

Closed nalsandori closed 8 months ago

nalsandori commented 8 months ago

image

slozier commented 8 months ago

The command line and C# program could have different default encodings. What value does f.encoding give you?

Is your file really using the cp949 encoding? If not you could pass in an explicit encoding, for example if it's utf-8: open(path, 'r', encoding='utf-8').

cp949 is not available because we don't have an implementation for _codecs_kr. Related issue: https://github.com/IronLanguages/ironpython2/issues/659

nalsandori commented 8 months ago
slozier commented 8 months ago

sys.getdefaultencoding() is not the encoding used by open. You can use locale.getpreferredencoding(False) to get that value, or open(filename).encoding.

You could also try utf-8 mode which causes open to default to utf-8:

var runtime = IronPython.Hosting.Python.CreateRuntime(new Dictionary<string, object> { { "Utf8Mode", true } });
nalsandori commented 8 months ago

Thank you very much for your kind explanation. The c# code has been helpful to me