Pythonã§ã³ãã³ããã¼ã«ãä½ã£ã¦ã¦ããã¤ãã§ã¤ãªãããããã¨ãã«è²ã
ã¯ã¾ã£ãã®ã§ã¡ã¢ã
WindowsXPã§Python2.7ã
# coding: utf-8 def main(): print u"ãã¹ã" # PYTHONIOENCODINGã§ã¨ã³ã³ã¼ãããã print "ãã¹ã" # ãã®ãã¡ã¤ã«ã®æåã³ã¼ã(utf-8) print u"ãã¹ã".encode('cp932') # cp932ã«ã¨ã³ã³ã¼ãããã if __name__ == '__main__': main()
ãããã³ãã³ãããã³ããããå®è¡ããã¨ãããªãã
>python test.py ãã¹ã ç¹ã»ãç¹ ãã¹ã
ãã®åºåããæååãããªãç¶æ
ã§ãã¡ã¤ã«ã«æ¸ãåºãããã
ã¡ãªã¿ã«ãåºåããªãã¤ã¬ã¯ããããã¨ããã¨ã¨ã©ã¼ã«ãªãã
>python test.py > test.txt Traceback (most recent call last): File "test.py", line 10, in <module> main() File "test.py", line 5, in main print u"ç¹ã»ãç¹ã» # PYTHONIOENCODING縺ァ繧ィç¹ï½³ç¹§ï½³ç¹ï½¼ç¹å³¨ï¼ç¹§å¾ï½ UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
PYTHONIOENCODINGã«cp932ãè¨å®ããã¨éã
1. Command line and environment — Python 3.5.1 documentation
>set PYTHONIOENCODING=cp932 >python test.py > test.txt >type test.txt ãã¹ã ç¹ã»ãç¹ã» ãã¹ã
ãã¦ããã§ã2è¡ç®ã®UTF-8ã®åãã¦ããã®ãèªããç¶æ
ã«ãããã
chardet ã¢ã¸ã¥ã¼ã«ã使ã£ã¦ãããªã¹ã¯ãªãããæ¸ããã
encfix-script.py
#!C:/Python27/python.exe import sys import chardet def main(): s = sys.stdin.read() for line in s.splitlines(): result = chardet.detect(line) sys.stdout.write(line.decode(result['encoding']).encode('cp932') + '\n') if __name__ == '__main__': main()
ãã¨ã¯ãsetuptoolsã®exe(easy_install.exeã¨ã)ã"encfix.exe"ã¨è¨ãååã§ã³ãã¼ãã¦ãã¹ã®éã£ãåããã£ã¬ã¯ããªã«ç½®ãã
>python test.py|encfix > test.txt >type test.txt ãã¹ã ãã¹ã ãã¹ã
æå¾ éãã®çµæãå¾ãããã
2011/11/21追è¨
æ¨æºã¨ã©ã¼åºåã夿ãããå ´åã¯ããªãã¤ã¬ã¯ãããã°è¯ãã
>python test.py 2>&1|encfix