2005-04-01ãã1ã¶æéã®è¨äºä¸è¦§
optparseã¢ã¸ã¥ã¼ã«ã使ã£ã¦èµ·åæã®å¼æ°ã®è§£æãè¡ãåºæ¬ã [argsOp.py] #!/bin/env python # -*- coding: shift_jis -*- import sys from optparse import OptionParser #å¼æ°ã®è¨å® parser = OptionParser() parser.add_option("-f", "--file", dest="filâ¦
é£çªãéãã¹ã¯ãªãããæ¸ããã®ã§ããã£ããã ããéããurlãããã¡ã¤ã«ããã¦ã³ãã¼ãããã¹ã¯ãªãããã¤ãã£ã¦ã¿ãããã¨æã£ãã®ã ããã©å·§ããããªãã urllibã®ãªãã¡ã¬ã³ã¹ã« urlretrieve( url[, filename[, reporthook[, data]]]) URL ã§è¡¨ããããâ¦
æ£è¦è¡¨ç¾ã®ç·´ç¿ã«é£çªurlãå±éãã¦è¡¨ç¤ºããã¹ã¯ãªãããä½ã£ã¦ã¿ãã http://www.doqn.ne.jp/65-32/cg/[10-20].html ã ã£ãã http://www.doqn.ne.jp/65-32/cg/10.html http://www.doqn.ne.jp/65-32/cg/11.html http://www.doqn.ne.jp/65-32/cg/12.html ã£â¦
ã¯ããã«URLãæ¸ããããã¹ããã¡ã¤ã«ãURL.txtããç¨æããã www.google.co.jp www.yahoo.co.jp www.microsoft.com/ URL.txtã®å 容ãèªã¿è¾¼ãã§ãããããã®ãµã¤ãã®ãããã表示ããã¹ã¯ãªãããä½ã£ã¦ã¿ãã [headget.py] #!/bin/env python # -*- codinâ¦
urllibã使ãã¨â¦â¦æ¥½ã #!/bin/env python # -*- coding: shift_jis -*- import urllib res = urllib.urlopen("http://www.google.co.jp/") print res.read()
httplibãã¤ãã£ã¦"GET"ãªã¯ã¨ã¹ã #!/bin/env python # -*- coding: shift_jis -*- import httplib conn = httplib.HTTPConnection("www.google.co.jp") conn.request("GET", "/index.html") res = conn.getresponse() print res.read()
ã¨ãããããããã¯ã¼ã¯ããã°ã©ãã³ã°ã£ã½ã ãã¨ããã£ã¦ã¿ãã åèï¼HTTP ãããã³ã« http://www.wakhok.ac.jp/~maruyama/W3serv/document/node92.html socketã使ã£ãåºæ¬çãªã¯ã©ã¤ã¢ã³ãããã°ã©ã googleã«"Get"ãªã¯ã¨ã¹ããéã£ã¦ã¿ã #!/bin/env pâ¦
ãã¡ã¤ã«ãä¸è¡ã¥ã¤èªã¿è¾¼ã¿ãè¡å ã«#ããã£ãã#以ä¸ãåé¤ãã¦è¡¨ç¤ºã åé¡ç¹ #ã®ã³ã¡ã³ãã«ãã対å¿ãã¦ããªãã ãprint "#ã³ã¡ã³ããããªãã"ãã®ãããªè¡ã§ã#以ä¸ãã³ã¡ã³ãã¨èªèãã¦ãã¾ãã [commentDe.py] #!/bin/env python # -*- coding: shiftâ¦
fileview.pyãè¡æ°ãä»ãã¦è¡¨ç¤ºããããã«å¤æ´ãã¦ã¿ã[fileline.py] #!/bin/env python # -*- coding: shift_jis -*- import sys argv = sys.argv[1:] for i in argv: print "[" + i + "]" for n, line in enumerate(open(i)): print "%d: %s" % (n,line) ,â¦
å¼æ°ã«æå®ããããã¡ã¤ã«ã®å 容ã表示ããã¹ã¯ãªãããä½ã£ã¦ã¿ãã [fileview.py] #!/bin/env python # -*- coding: shift_jis -*- import sys argv = sys.argv[1:] for i in argv: print "[" + i + "]" files = open(i,'r') lines = files.readlines() foâ¦
sysã¢ã¸ã¥ã¼ã«ã®argv[]ãªã¹ãã§å¼æ°ãå¾ãããã argv[0]ã¯èµ·åããpythonã¹ã¯ãªãããã¡ã¤ã«ã®ååãå ¥ãã C:\Python23jp>test.py aaa bbb ccc ã®å ´åã¯ã argv[0] = "C:\Python23jp>test.py" argv[1] = "aaa" argv[2] = "bbb" argv[3] = "ccc" ã£ã¦ãã¨ãâ¦
>>> a = 10 >>> type(a) >>> a = "test" >>> type(a) >>> a = [1, 2, 3] >>> type(a) >>> a = (1, 2, 3) >>> type(a) >>> a = {1:1, 2:2, 3:3} >>> type(a) >>> def b(): print "test" >>> a = b >>> type(a) >>> import urllib >>> a = urllib >>> type(a) â¦
ã¡ã½ãããããããªããã£ã¦ã¨ãã¯dir() >>> import urllib >>> x = urllib.urlopen("http://www.google.ne.jp") >>> dir(x) ['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close', 'fileno', 'fp', 'geturl', 'headers', 'info', 'next'â¦
対話ã¢ã¼ãã®æãimport ããã¢ã¸ã¥ã¼ã«ã®ãã«ããè¦ããã >>>import string >>>help(string) ã§ã¢ã¸ã¥ã¼ã«ã®ãã«ãã表示ãããã(è±èªã ãã©ï¼ï¼ï¼)
â¼ä½¿ãæ¹ ä»¥ä¸ã¯ãcãã©ã¤ãç´ä¸ã«test.txtãåå¨ããã«ã¬ã³ããã£ã¬ã¯ããª(ä½æ¥ãã©ã«ã)ã"C:"ã§ãããã¨ãåæ (Pythonã¯"c:python23"ã«ã¤ã³ã¹ãã¼ã«ããã¦ããç¶æ ãåæ) ãªããPython Shellä¸ã§ã¯[ï¿¥]è¨å·ã¯ããã¯ã¹ã©ãã·ã¥(\)ã§è¡¨ç¤ºãããã ([c:¥¥â¦
ãã¡ã¤ã«æä½ã®æé 㯠ï¼ï¼ãã¡ã¤ã«éã(ãªã¼ãã³) ï¼ï¼ãã¡ã¤ã«å¼ã(èªãã ããæ¸ããã) ï¼ï¼ãã¡ã¤ã«éãã(ã¯ãã¼ãº) ãã¡ã¤ã«ã®ãªã¼ãã³ã«ã¯file()ã使ãã æ¸å¼ã¯ã file("ãã¡ã¤ã«å", "ãã©ã°") open("ãã¡ã¤ã«å", "ãã©ã°")ã¯ãfile("ãã¡ã¤ã«å",â¦
ã¢ã¸ã¥ã¼ã«ã¯èªåã§ä½ããã¨ãã§ããããã©ãPythonã«ã¯æ¨æºã§ã¤ãã¦ããã¢ã¸ã¥ã¼ã«ãé常ã«è±å¯ãªã®ã§ãã¡ãã£ã¨ãã使ãæ¨ã¦ã¹ã¯ãªãããä½ãã¨ãã«ã¯ã»ã¼äºè¶³ããã åè: 10. æ¨æºã©ã¤ãã©ãªã®ç°¡åãªãã¢ã¼ http://www.python.jp/doc/release/tut/node12â¦
é¢æ°ãã¯ã©ã¹ãä¿åããã¹ã¯ãªãããã¡ã¤ã«ã¯importãããã¨ã§ãå¥ã®ã¹ã¯ãªããã§ä½¿ç¨ã§ããã â¼ä½¿ãæ¹ ä»¥ä¸ãtest.pyã¨ããååã§ä¿å #!/bin/env python # -*- coding: shift_jis -*- class Arithmetic: messge = "ã¡ã½ããå®è¡" def __init__(self): prinâ¦
pythonã®ã¯ã©ã¹ã§ã¯å®ç¾©ãã¦ããªãèå¥å(å¤æ°ãªã©)ãæå®ããã¨èªåçã«ç»é²ããã¦ãã¾ãã â¼ä¾ >>> ================================ RESTART ================================ >>> class test1: a = 0 b = 0 def testprint(self): print self.a + self.â¦
ã¯ã©ã¹ã®ä¸ã§å¤æ°ã使ãã¨ãã¯ãã¤ã³ãã³ã(åä¸ã)ã«ãã£ã¦åºåããããããã¯ã®é層ã«æ³¨æã â¼ä¾ >>> ================================ RESTART ================================ >>> class test: #ããã¯éå±¤ï¼ a = "test" def testprint(self): #ããâ¦
ã¯ã©ã¹ã¯ãå¤æ°ã¨ãããæä½ããé¢æ°ãã¾ã¨ãã¦åå(ã¯ã©ã¹å)ãä»ãããã®ã æä½ããé¢æ°ç¾¤ãã¡ã½ããã¨å¼ã¶ â¼æ¸å¼ class ã¯ã©ã¹å(ç¶æ¿ããã¯ã©ã¹å): ã¡ã³ãã¼å¤æ°ã®å®ç¾© def __init__(å¼æ°): æï¼ def ã¡ã½ããåï¼(å¼æ°): æï¼ def ã¡ã½ããåï¼(å¼æ°)â¦
é¢æ°ã¨ããã®ã¯ããã¼ã¿ãåãåãå®ããããå¦çãå®è¡ãã¦çµæãè¿ãå½ä»¤ç¾¤ã é »ç¹ã«ä½¿ãå¦çãåå(é¢æ°å)ãä»ãã¦å®ç¾©ãã¦ããã°ãã¹ã¯ãªããå ã§ãã¤ã§ãå¼ã³åºãã¦ä½¿ããã â¼æ¸å¼ def é¢æ°å(å¼æ°ã®ãªã¹ã): æï¼ return é¢æ°ãè¿ããã¼ã¿ é¢æ°ãå¼ã³â¦
ä¾å¤å¦çã¨ãã¨ã©ã¼ãã©ãããªã©ã¨å¼ã°ãããã¤ã â¼æ¸å¼ try: ãã®ä¸ã§ã¨ã©ã¼ãèµ·ããã except: ããã®å¦çãè¡ã â¼ä½¿ãæ¹ >>> ========================= RESTART ========================= >>> def test1(a, b): c = a + b print c >>> test1(1, "hello"â¦
ã¤ãã¬ã¼ã¿ã¨ã¯ï¼ æ¥æ¬èªã§ã¯ãå復åããªã©ã¨è¨³ãããã é åã®ãããªå¹¾ã¤ãã®å¤ã並ãã§ãããã¼ã¿ã«å¯¾ãã¦å¤ã®ã²ã¨ã¤ãæãã表示ãããã次ã®å¤ã«é²ããªã©ã®æ©è½ããã¤ã ã¾ããå®éã«ä½¿ã£ã¦ã¿ãæ¹ã解ãããããPythonã®çµã¿è¾¼ã¿ãã¼ã¿å(ãªã¹ããã¿ãâ¦
â¼æ¸å¼ for å¤æ° in ã·ã¼ã±ã³ã¹: æï¼ else: æï¼ ã·ã¼ã±ã³ã¹(æååããªã¹ããã¿ãã«ãè¾æ¸ãªã©)ã®å¤ãå¤æ°ã«ä»£å ¥ããªããé 次ãæï¼ãå®è¡ã ã·ã¼ã±ã³ã¹ãåããã£ããelseãããã¯ã®æï¼ãå®è¡â¼ä½¿ãæ¹ for count in [0, 1, 2, 3, 4]: print count, "åç®" â¦
â¼æ¸å¼ while å¼: æï¼ else: æï¼ å¼ãçã§ãããããæï¼ãç¹°ãè¿ãã else:ãããã¯ã®æï¼ã¯çµäºæã«ä¸åã ãå®è¡ããã â¼ä½¿ãæ¹ count = 0 while count â¼å®è¡çµæ 0 åç® 1 åç® 2 åç® 3 åç® 4 åç® 5 åç® 6 åç® 7 åç® 8 åç® 9 åç® 0 ãªã»ããããâ¦
â¼æ¸å¼ if å¼ï¼: æï¼ elif å¼ï¼: æï¼ else: æï¼ å¼ï¼ã®å¤ãçã§ããã°æï¼ãå®è¡ å¼ï¼ã®å¤ãå½ã§å¼ï¼ã®å¤ãçã§ããã°æï¼ãå®è¡ å¼ï¼ãå¼ï¼ã®å¤ãå½ã§ããã°æï¼ãå®è¡ â¼ä½¿ãæ¹ test = 1 if test == 0: print "å¤æ°testã®å¤ã¯ï¼" elif test == 1: print "â¦
Pythonã®æ§é æã¯ã¤ã³ãã³ã(åä¸ã)ã«ãã£ã¦ãããã¯ã表ãã¾ãã æ¬å¼§({})ãBEGINã»ENDã¯ããã¾ããã for count in [0, 1, 2, 3]: #ææ«ã«[:]ãä»ããã®ãå¿ããã«ã print "ããã¯foræã®ãããã¯ã®ä¸" print "ããã¯foræã®ãããã¯ã®å¤"
対話ã¢ã¼ã(python shell)ã§å ¥åã ã·ã£ã¼ã[#]以ä¸ã¯ã³ã¡ã³ããªã®ã§ãå ¥åããªãã¦ããããâ¼å¤æ°(æ°å) >>> a = 1 #aã¨ããå¤æ°ã«1ãæ ¼ç´ãã¦ãã >>> A = 10 #大æåã¨å°æåã¯éãå¤æ°ã¨ãã¦æ±ããã >>> print a 1 >>> print a, A 1 10 >>> b = -1 >>> câ¦
ã·ã£ã¼ãã#ãããè¡æ«ã¾ã§ãã³ã¡ã³ãã«ãªãã >>>print "Hello World" #ããããã³ã¡ã³ã Hello Worl è¤æ°è¡ã«ããã£ã¦ã³ã¡ã³ããè¨è¿°ãããã¨ãã¯ã"""ãã使ãã print "ããã¯å®è¡ããã" """ ããã¯ã³ã¡ã³ã ãããã³ã¡ã³ã """ print "ãããå®è¡ããâ¦