ã¨ã³ã³ã¼ãã£ã³ã°æå®
å ¥åºåå¨ãã®ã¨ã³ã³ã¼ãã£ã³ã°ã®è¨å®ã®æ¹æ³ãæ¸ããã¦ããããã®ä¾ã§ãã¨ã³ã³ã¼ãã£ã³ã°ãåå¾ããååã¨å ¥åºåã®ã¨ã³ã³ã¼ãã£ã³ã°ãè¨å®ããå¾åã«åãã¦èããã
import sys, locale, codecs
enc = locale.getpreferredencoding()
sys.stdout = codecs.getwriter(enc)(sys.stdout)
sys.stdin = codecs.getreader(enc)(sys.stdin)
ãPythonãã¥ã¼ããªã¢ã«ãã«ã¯ä»¥ä¸ã®ã³ã¼ããè¼ã£ã¦ããã
import codecs, sys sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)
å¾åé¨åã¯ã以ä¸ã®ããã«åããã¨ããã£ã¦ããã ãã
>>> import codecs >>> codecs.lookup('utf-8') <codecs.CodecInfo object for encoding utf-8 at 0x1437d88> >>> list(codecs.lookup('utf-8')) [<built-in function utf_8_encode>, <function decode at 0x014E37F0>, <class encodings.utf_8.StreamReader at 0x0143BED0>, <class encodings.utf_8.StreamWriter at 0x0143BEA0>] >>> >>> codecs.getwriter('utf-8') <class encodings.utf_8.StreamWriter at 0x0143BEA0>
ååã®locale.getpreferredencoding()ã¯ãã¦ã¼ã¶ã使ç¨ãã¦ããã¨æ¨æ¸¬ãããcharsetãåå¾ãã¦ãããããããã®ã¾ã¾æå®ããå ´åã«åé¡ãçºçããã±ã¼ã¹ãæ¸ããã以ä¸ã®è¨äºãè¦ã¤ããã
- METAREAL: Python: Mac OS X ã§ã® locale.getpreferredencoding()
- METAREAL: Apple ã«ãããã±ã¼ã«å¨ãã®å¤æ´ç¹ã Python 2.5.1 ã«ãé©ç¨ãã
Tracã§ã¯ãååé¨åã以ä¸ã®ããã«ãã¦ããã¿ãããä½ããåãªãå®å
¨å¯¾çã£ã½ãã®ã§ãã¾ãæ¬è³ªçã§ã¯ãªãããã
...(ç¥)... encoding = locale.getpreferredencoding() or sys.getdefaultencoding() if sys.platform != 'win32': encoding = locale.getlocale(locale.LC_TIME)[1] or encoding # the above is broken on win32, e.g. we'd get '437' instead of 'cp437' ...(ç¥)...
ä½ããæ°ã«ãªã£ãã®ã¯ãMAC OS Xã§Python 2.5.1ãã½ã¼ã¹ããã¤ã³ã¹ãã¼ã«ããå ´åã«çºçãã以ä¸ã®ãããªåé¡ç¹ã
- locale.getpreferredencoding() ã®è¿ãã¨ã³ã³ã¼ãã£ã³ã°ã X-MAC-JAPANESE ã«ãªã£ã¦ãã¾ã
- Python ã§ã¯ X-MAC-JAPANESE ãããã©ã«ãã§ã¯ãµãã¼ãããã¦ããªããããlocale.getpreferredencoding() ãå©ç¨ããã¨ã³ã³ã¼ãããã³ã¼ããã§ããªã
è¨äºã«æ¸ããã¦ããã®ã¯åé¿æ¹æ³ã¨ãã¦ãlocale.pyã«ããããå½ã¦ãã¨ããæ¹æ³ã ã£ãããããã«ããgetpreferredencoding()é¢æ°å®ç¾©ã®åå²ãå¤æ´ããããã¦ããã
ã¾ã¨ããã¨ãodz bufferããã®æ¹æ³ã¯ã確ãã«ãã¹ããã©ã¯ãã£ã¹ãããããªããä½ããgetpreferredencoding()é¢æ°ã®å 容ã«ãã£ã¦ã¯Pythonã§ä½¿ç¨ã§ããªãencodingãè¿ã£ã¦ãã¾ãLookupErrorãçºçããå¯è½æ§ã¯ããã¨ãããã¨ã¯æèãã¦ããå¿ è¦ã¯ããã¨æãã
以ä¸ã®2ã¤ã®ã©ã¡ããã®å¯¾çã¯å¿ è¦ãããããªãã
- encãã¢ããªãã¨ã«ã¦ã¼ã¶ãèªåã§'utf-8'ã'cp932'ãªã©ã¨ç´æ¥æå®ãã
- LookupErrorãåºãå ´åã¯ã¦ã¼ã¶ã«locale.pyã«ããããå½ã¦ã¦ããã
YAMLã¨JSON
Wikipediaããã®å¼ç¨ã
YAMLï¼ã¤ã ã«ï¼ã¨ã¯ãæ§é åãã¼ã¿ããªãã¸ã§ã¯ããæååã«ã·ãªã¢ã©ã¤ãºï¼ç´ååï¼ããããã®ãã¼ã¿å½¢å¼ã®ä¸ç¨®ã
JSONï¼ã¸ã§ã¤ã½ã³ãJavaScript Object Notationï¼ã¯ãJavaScriptã«ããããªãã¸ã§ã¯ãã®è¡¨è¨æ³ããã¼ã¹ã¨ãã軽éãªãã¼ã¿è¨è¿°è¨èªã§ããã
Pythonã®æ¨æºã©ã¤ãã©ãªã§YAMLã¨JSONãæ±ããã®ãæ¢ãã¦ã¿ããæ¨æºã«ã¯ãªãã£ã½ããJSONã¯ãJavaScriptã§ãã¡ã¤ã«å ã®æååãeval()ããã°ãã®ã¾ã¾ãªãã¸ã§ã¯ãã«å¤æããããåãçºæ³ã§Pythonã§ãparams.pyãç¨æãã¦ãimport paramsããã°è¯ãã¨ããããæ¹ã§ãè¯ãã®ã§ã¯ãªããï¼åã¥ãã¦PySON (ãã¤ã½ã³ãPython Script Object Notation)ã
ã¨ãããä»äºã§ãã®ããæ¹ããã¦ããã®ã ãã注æç¹ã¨ãã¦ã¯æ¥æ¬èªã®æ±ããpy2exeã§exeåããéã«ä¸ç·ã«params.pyãå«ããªãããã«æ°ãä»ããã ãã§ãããå ¨ãåé¡ãªããä¸çªæã£åãæ©ããããã©ã¡ã¼ã¿ç¨ã®ãã¡ã¤ã«ãåãè¾¼ãã§ããã ããªã®ã§ãããã»ã©é»éè¡çãªè¨³ã§ããªããã¨ãããJSONã§ãã»ã¨ãã©åããPySONã§ã¯ãimportããåã«åçã«sys.pathã«ãã¹ã追å ãã§ããã®ã§ãã¹è¨å®ãåé¡ãªãã
Pythonã®æ¨æºã©ã¤ãã©ãªã«åå¨ããªãã¨ãããã¨ã¯å¿ è¦æ§ãä½ãã¨è¨ããã®ãããããªãã