Closed nalsandori closed 10 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
I excuted the following commands in each program respectively.
>>>sys.getdefaultencodnig()
--> 'utf_8'
The same results were displayed on both the ironpython console and c# program
The following code also causes an encoding error in c# program(based on ironpython 3.4.1)
os.popen(uabswitchcmdpath + ' -v').read()
--> the above command worked fine in a c# program based on ironpython 2.7.9
Here is the c# initialize code
var runtime = IronPython.Hosting.Python.CreateRuntime();
runtime.IO.SetErrorOutput(streamwriter, Encoding.Default);
runtime.IO.SetOutput(streamwriter, Encoding.Default);
runtime.IO.SetInput(streamwriter, Encoding.Default);
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 } });
Thank you very much for your kind explanation. The c# code has been helpful to me
with open(path, 'r') as f: content = f.read() #problem line