Handling of character code of file in IronPython

Reading and writing sjis files. The final result is quite normal, but ...

First of all, there is an example using codecs.getreader / writer when you go around normally.

So not possible. In conclusion, use string.encode (), decode ()

Read:

for line in open('file.txt', 'rt'):
    linedec = line.decode('cp932')
   ...

Also, writing is

str(a_unicode_string)

Due to the nice spec orz that results in UnicodeEncodeError, untyped objects When handling it, you cannot rely on the default operation, and you need to convert it to a string by yourself. Why is this part notation?

'%s' % obj

You can do it. (I think it can be the same as the behavior str () of this code)

Also, when I try to write a unicode string to a file that I opened without setting any character code, I get a UnicodeEncodeError. Moreover, if you open it in text mode, it will occur at flush time instead of writing it, so error recovery will not be possible, which is quite a problem. It doesn't seem to treat it as a UTF16 binary file.

So export:

f=open('sjis.txt', 'wt')
lineenc = linestr.encode('cp932')
print >>f, lineenc
print >>f, ('%s' % some_object).encode('cp932')

Recommended Posts

Handling of character code of file in IronPython
[Python] Get the character code of the file
[Python] [chardet] Automatic detection of character code of file
Convert the character code of the file with Python3
Handling of quotes in [bash]
2.x, 3.x character code of python
Character code learned in Python
Handling of JSON files in Python
The story of the "hole" in the file
Spit out a list of file name, last modified date and character code in python3
Character code
Use of constraints file added in pip 7.1
Read the file by specifying the character code.
Character code conversion of CSV file using Loop (Shift JIS to UTF8)
python character code
Uncertainty of Japanese unide code in Tacotron 2 series
Links and memos of Python character code strings
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
Speed evaluation of CSV file output in Python
[Unity (C #), Python] Try running Python code in Unity using IronPython
A collection of code often used in personal Python
Store Japanese (multibyte character string) in sqlite3 of python
# Function that returns the character code of a string
Ruby, Python code fragment execution of selection in Emacs
List of Python code used in big data analysis
Quickly list multiple lines of text in your code
File operations in Python
File processing in Python
Handling json in python
About Python3 character code
File operations in Python
Error handling in PythonBox
Hexadecimal handling in Python 3
Reimport of .py file
Code that sets the default value in case of AttributeError
Scraping the Excel file of the list of stores handling regional coupons
Comparison of exponential moving average (EMA) code written in Python
[Note] Import of a file in the parent directory in Python
Decrypt one line of code in Python lambda, map, list
Mass generation of QR code with character display by Python
Comparison of data frame handling in Python (pandas), R, Pig
Handling of HSV color space lower and upper in OpenCV
Google search for the last line of the file in Python
Command to list all files in order of file name