Kuwataããããæçã®ããã¾ã«ã¯ã·ã¯ã·ã¨Pythonã§ããã°ã©ãã³ã°ãã¦ãã¦ãåã¯çºãã¦ããã ãã§ããçºããããã«ãä¸å¿Pythonã³ã¼ããèªããã»ããããããªãã¨æã£ãã®ã§ï¼ã¤ããµã©ããï¼èªåï¼ãPython 2.6 ãããã£ã¦ã¿ã¾ããã
対話çå¦çç³»ãã·ã§ã«ã¨ãã¦ä½¿ã
対話çå¦çç³»ãèµ·åãã¦ãã¨ããããæ®éã®ã·ã§ã«ã®ããã«ä½¿ããããã ãã©ãã©ãããããããã ããï¼ -- osã¢ã¸ã¥ã¼ã«ã®é¢æ°ã使ãã°ããããã§ãã
>>> import os
>>> os.getcwd()
'C:\\tmp'
>>> os.listdir(".")
['#dump.c#', 'a.exe', ...(çç¥)...]
>>> os.mkdir("pytest")
>>> os.chdir("pytest")
>>> os.getcwd()
'C:\\tmp\\pytest'
>>> os.listdir(".")
[]
ã¡ãã£ã¨ããã¼ã¿ã¤ãã大å¤ã ãªã次ã®ãããªé¢æ°ãæ¸ãããã¡ã¤ã«ãä½ãã¾ããã
# -*- coding: utf-8 -*-
# shellcmd.pyu'''è¶ ç°¡åãªã·ã§ã«ã³ãã³ã'''
import os
def pwd():
print os.getcwd()def cd(dir):
os.chdir(dir)def ls():
for f in os.listdir('.'):
print fdef mkdir(dir):
os.mkdir(dir)
import shellcmd
ããã¨å®ç¾©ããé¢æ°ã使ãã¾ãããããã ã¨ãshellcmd.ls()
ã¨æ¸ãããæå㧠from shellcmd import ls
ã¨ãããªãã¨ãããªãããã«ã¿ãããã¨æã£ãããç°å¢å¤æ°PYTHONSTARTUPã使ãã¨ã対話çå¦çç³»ã®ã¹ã¿ã¼ãã¢ããã»ã¹ã¯ãªãããæå®ã§ãããããªã®ã§ãã¨ãããã次ã®ããã«ãã¦èµ·åã
set PYTHONSTARTUP=shellcmd.py
python
ls() ã¨ããããã«ä½¿ãã¾ããã対話çã¢ã¼ãã«ããã¦ã³ãã³ãã¨ãã¦æç¨ãªé¢æ°ç¾¤ã¯ã%HOME%\startup.py ã¨ã ~/.pythonrc.py ã¨ãã«ã¾ã¨ãã¦ãç°å¢å¤æ°PYTHONSTARTUPã«æå®ãã¦ããã¨ããããã§ãã
JSONå¦çã©ã¤ãã©ãª
Python 2.6 ã§ã¯ãæ¨æºã§JSONãã³ããªã³ã°ããµãã¼ãããã¦ããããã§ããjsonã¢ã¸ã¥ã¼ã«ç¾¤ã®å®ä½ã¯ãPython26/Lib/json/ ã«ç½®ããã¦ãã¾ãããimportãã¦ã¿ã¾ããã
>>> import json
>>> help(json)
Help on package json:
NAME
json - A simple, fast, extensible JSON encoder and decoder...(以ä¸çç¥)...
jsonã©ã¤ãã©ãªã®ããã¥ã¡ã³ãã¯Webã«ãããã¾ã â http://docs.python.org/library/json.html
ãã¼ã¸ã§ã³ 2.5 ã§ã使ããsimplejsonã£ã¦ã®ãããã¾ãã
æ¨æºjsonã¨simplejsonã®ä½µç¨ã¯æ¬¡ã®ããã«ããã¨ãããã
# simplejsonãåªå ããå ´å
try:
import simplejson as json
except ImportError:
import json
jsonã©ã¤ãã©ãªã®ããã¥ã¡ã³ãã«ããã¨ãJSONãã¼ã¿ã¯ã次ã®ããã«Pythonãã¤ãã£ããã¼ã¿ã«ãããããã¾ãã
JSON | Python | |
---|---|---|
object | dict | |
array | list | |
string | unicode | |
integer | int, long | |
number (not integer) | float | |
boolean | bool | |
null | NoneType |
ããã¯ãé常ã«ç´ ç´ãªãããã§ããã
JSONæååã®è§£æã¨å¤æ
ã¨ããããå®é¨ã
>>> import json
>>> json_str = '{"name" : "æ¿æ±ãã³å", "age" : 26}'
>>> print json_str
{"name" : "æ¿æ±ãã³å", "age" : 26}
>>> json.loads(json_str)
Traceback (most recent call last):
File "", line 1, in
File "c:\Installed\Python26\lib\json\__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "c:\Installed\Python26\lib\json\decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\Installed\Python26\lib\json\decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "c:\Installed\Python26\lib\json\scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "c:\Installed\Python26\lib\json\decoder.py", line 183, in JSONObject
value, end = iterscan(s, idx=end, context=context).next()
File "c:\Installed\Python26\lib\json\scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "c:\Installed\Python26\lib\json\decoder.py", line 155, in JSONString
return scanstring(match.string, match.end(), encoding, strict)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x94 in position 0: unexpect
d code byte
ã¢ãªãï¼ Windowsã®ã³ãã³ãããã³ããããã ã¨ãæ¥æ¬èªãã·ããJISã§å ¥ã£ã¦ããããã§ããJSONãã¼ã¿ã®ããã©ã«ãã¨ã³ã³ã¼ãã£ã³ã°ã¯UTF-8ã ãããã¨ã³ã³ã¼ãã£ã³ã°ã®ãã¹ãããã§ãã¾ããã£ã¦ãªããããã
>>> json_str
'{"name" : "\x94\xc2\x93\x8c\x83g\x83\x93\x8bg", "age" : 26}'
16é²ã§è¦ãã¨ãâ¦â¦ ã¦ã¼ã ããããããªããã©ãUTF-8ã£ã½ããªãã§ãããjson.loadsã®ç¬¬äºå¼æ°ã«æåã¨ã³ã³ã¼ãã£ã³ã°ãæå®ã§ããã®ã§ï¼
>>> json.loads(json_str, 'Shift_JIS')
{u'age': 26, u'name': u'\u677f\u6771\u30c8\u30f3\u5409'}
ãã¾ããã£ãã¿ãããå¾ããããã¼ã¿ã§ã¯ãæååã¨ãã¦ãPythonã®unicodeãã¼ã¿ã使ããã¦ãã¾ãã
JSONãã¡ã¤ã«ã®ãã¼ã
ä»åº¦ã¯ãã¡ã¤ã«ããJSONãã¼ã¿ãèªã¿è¾¼ãã§ã¿ã¾ãã次ã®ãããªãã¡ã¤ã«ãä½ãã¾ãããã¨ã³ã³ã¼ãã£ã³ã°ã¯UTF-8ã§ãã
{
"name" : "æ¿æ±ãã³å",
"age" : 26
}
ãã¡ã¤ã«ããæååï¼ã¤ãããã¤ãåï¼ãä½ã£ã¦ãããã json.loads()ï¼æå¾ã®sã¯stringã®sã ã¨æãããï¼ã§å¦çãã¦ãããã®ã§ãããjson.load() ã使ãã°ãã¡ã¤ã«ããJSONãã¼ã¿ããã¼ãã§ãã¾ãã
>>> f = open("tonkichi.json")
>>> json.load(f)
{u'age': 26, u'name': u'\u677f\u6771\u30c8\u30f3\u5409'}
>>> f.close()
次ã®ãããªé¢æ°ãä½ã£ã¦ããã°ä¾¿å©ã§ãããã
def load_json(fname, encoding = 'utf-8'):
with open(fname) as f:
return json.load(f, encoding)
withæ§æã¯ãPython 2.6 ãããµãã¼ãããããã®ã§ãï¼ã¨ããã«ãã*1ä»è¾¼ã¿ã®ç¥èã§è¨ã£ã¦ã¿ã ^^;ï¼ã
*1:7åãããåã