ç¹æ®ãã©ã«ãã®ãã¹ãå¾ã
WScript.Shellãªãã¸ã§ã¯ãã§ã¯SpecialFoldersããããã£ãå¾ããã¨ãã§ããã
ãã®ããããã£ã¯windowsã®ç¹å¥ãªãã©ã«ã(ãã¹ã¯ãããããã¤ããã¥ã¡ã³ããªã©)ã®ãã¹ãå¾ããã¨ãã§ããã
>>> import win32com.client >>> com = win32com.client.Dispatch("WScript.Shell") >>> com.SpecialFolders("MyDocuments") #ãã¤ããã¥ã¡ã³ãã®ãã¹ãåå¾ u'C:\\My Documents' >>> com.SpecialFolders("Favorites") #ããæ°ã«å ¥ãããã©ã«ãã®ãã¹ãåå¾ u'C:\\WINDOWS\\Favorites' >>> com.SpecialFolders("Recent") #ãæè¿ä½¿ã£ããã¡ã¤ã«ãã®ãã¹ãåå¾ u'C:\\WINDOWS\\Recent'
windowsã®ä½¿ãæ¨ã¦ã¹ã¯ãªãããæ¸ãã¨ãã«é常ã«ä¾¿å©ã
åè:1ï¼ç¹æ®ãã©ã«ãã®åå¾ ââSpecialFoldersããããã£ââ
http://www.atmarkit.co.jp/fwin2k/operation/wsh06/wsh06_01.html
windows COMãªãã¸ã§ã¯ããæä½ãã
win32allã¢ã¸ã¥ã¼ã«ã®ä¸ã®win32com.clientã¯ãwindowsã®COM(Component Object Model)ãªãã¸ã§ã¯ããæä½ããæ¡å¼µã¢ã¸ã¥ã¼ã«ã
Python Win32 Extensions(win32all)
http://starship.python.net/crew/mhammond/win32/Downloads.html
ã³ã¬ã使ããã¨ã§WSHã®ããã«ããªã¼ãã¡ã¼ã·ã§ã³æ©è½ãæã£ãCOMãªãã¸ã§ã¯ããPythonã¹ã¯ãªããããå¶å¾¡ãããã¨ãã§ããã
IEãExcelã®ä½æ¥ãèªååããã¹ã¯ãªãããªãããæ¸ãã¨ãã«ä¾¿å©ã
(å½ç¶ã®ãã¨ãªããwin32comã¯windowsä¾åã)
>>> import win32com.client >>> com = win32com.client.Dispatch("WScript.Shell") >>> com.Run("notepad.exe") #ãã¼ãããã(ã¡ã¢å¸³)ã®èµ·å 0åè:第5åãWshShellãªãã¸ã§ã¯ãã®è©³ç´°ï¼1ï¼ http://www.atmarkit.co.jp/fwin2k/operation/wsh05/wsh05_01.html
æ¥æ¬èªã§æ£è¦è¡¨ç¾ã使ã
Shift-jisã§ã¯å¤ã«ããã¯ã¹ã©ãã·ã¥(=0x5c)ãå«ã¾ããå ´åãããã(ä¾ãã°"表"ã®å)
Pythonã§ã¯ããã¯ã¹ã©ãã·ã¥ã¯ã¨ã¹ã±ã¼ãæåã¨ãã¦æ±ãããã®ã§ããã®ãããªæåãæ£è¦è¡¨ç¾ã®ãã¿ã¼ã³ã«å«ã¾ãã¦ããã¨æå³ããªãçµæã¨ãªãã
æååããã£ããunicodeã«å¤æãããã¨ã§ããã®åé¡ãåé¿ã§ããã
â¼ä¾ï¼
>>> ================================ RESTART ================================ >>> import re >>> string = "表示ã§ãã¾ãã" >>> print re.sub('表示', "ãé¡ã", string) 表示ã§ãã¾ãã #ã¡ããã¨å¤æãããªã >>> patten = "表示" >>> print re.sub(patten.decode("shift-jis"), "ãé¡ã",string.decode("shift-jis")) ãé¡ãã§ãã¾ãã #ã¡ããã¨å¤æããã¦ãã
â¼ä¾ï¼(yahooããåå¾ããã½ã¼ã¹ãæ£è¦è¡¨ç¾ã§ãµã¼ãããã)
ãç°å¢:Yahooã®ãã¼ã¸ã¯euc-jpãPython Shellã¯shift-jis
ã対象ã¨ãªãæåãã¼ã¿(ããã§ã¯å¤æ°html)ã®æåã³ã¼ãã解ã£ã¦ããäºãåæã
>>> ================================ RESTART ================================ >>> import urllib2 >>> urlhandler = urllib2.urlopen("http://www.yahoo.co.jp/") >>> html = urlhandler.read() >>> import re >>> pattern = "å 責äºé " >>> print re.search(pattern.decode("shift-jis"), html.decode("euc_jp")).group() å 責äºé
åè:æ¥æ¬èªç°å¢ã§ã®Python (for Python 2.3 or later)
http://www.python.jp/Zope/articles/japanese/Python4Japanese-2
ãªã³ã¯å
ã§ã¯ãunicodeé¢æ°ã使ã£ã¦ããã®ã ããã©ãå人çã«ã¯æååã®ã¡ã½ããã«çµ±ä¸ããæ¹ãç¾ããæ°ãããã
(ã¾ããæ°åã®åé¡)
>>> ================================ RESTART ================================ >>> string = "表示ã§ãã¾ãã" >>> unicode(string, "shift-jis") u'\u8868\u793a\u3067\u304d\u307e\u3059\u304b' >>> string.encode("shift-jis") '表示ã§ãã¾ãã'
>>> ================================ RESTART ================================ >>> string = "表示ã§ãã¾ãã" >>> string.decode("shift-jis") u'\u8868\u793a\u3067\u304d\u307e\u3059\u304b' >>> string.encode("shift-jis") '表示ã§ãã¾ãã'
åè:2.3.6.1 æååã¡ã½ãã
http://www.python.jp/doc/release/lib/string-methods.html#l2h-175
æåã³ã¼ãå¤æ
>>>import pykf >>>import urllib >>>url = "http://www.yahoo.co.jp" >>>urlhandler = urllib.urlopen(url) >>>html = urlhandler.read() >>>html = pykf.tosjis(html, pykf.EUC) >>>print html
yahooãã¼ã¸ã®æåã³ã¼ãã¯ãEUCããªã®ã§ãSJISãã«å¤æãã
html = pykf.tosjis(html, pykf.EUC)
å ãã¼ã¿ã®æåã³ã¼ããå¤ããªãã¨ãã¯èªåå¤å¥
html = pykf.tosjis(html)
TCPWatchã使ã£ã¦ããªã¯ã¨ã¹ãã®ä¸èº«ã確èªããªãããã¹ã
http://hathawaymix.org/Software/TCPWatch
ã³ãã³ãã©ã¤ã³ããTCPWatchãèµ·åããã
c:>tcpwatch.py -p 127.0.0.1:3128
TCPWatchãset_proxyãã¦ãªã¯ã¨ã¹ããé£ã°ã
>>>import urllib2 >>>req = urllib2.Request("http://www.google.co.jp") >>>req.set_proxy("127.0.0.1:3128", "http") >>>req.add_header("User-agent", "python") >>>req.add_header("Pragma", "no-cache") >>>site = urllib2.urlopen(req)