<dfn id='h1ICnGUP'></dfn>

        <noscript id='h1ICnGUP'></noscript>

      1. ƵĿ
        ҳ > 򿪷 > Web > Python >
        Pythonģļݴ-json֪ʶ
        2019-02-21 15:40:12         Դ޷ӵIJ  
        ղ   ҪͶ

        /home/kioskһ.쳣

        1.쳣-01

        򿪷ijЩִвȷ﷨ȫȷ
        try쳣
        try
         ִеĴ
        expect
         ִĴ

        ʵ

        try:
         num=int(raw_input('дһ'))
        except:
         print 'һȷ'
        #Ƿ򶼻ִ
        print '*' * 50

        ȷ

        дͼƬ

        

        дͼƬ
        2.쳣-02

        python׳쳣ʱһдϢĵһǴ

        ʵ

        #1.ʾûһ
        #2.ʹ6û
        try:
         num = int(raw_input('һ'))
         result = 6 / num
         print result
        except ZeroDivisionError:
         print '0 0'
        except ValueError:
         print 'ȷ'
        
        #1.IJ
        #2.0

        Ľ
        дͼƬ
        дͼƬ
        ȷĽ
        дͼƬ
        3.쳣-03

        δ֪
        ڿʱҪжпֵܳĴһѶȵ
        ϣ۳κδpython׳쳣ֹ
        һexcept

        ʵ

        try:
         num = int(raw_input('һ'))
         result = 6 / num
         print result
        
        #δ֪쳣
        #resultһ
        except Exception as result:
         print 'δ֪ %s' % result

        Ľ
        дͼƬ
        дͼƬ
        ȷĽ
        дͼƬ
        4.쳣-04

        ʽ
        try:
         #ִеĴ
         pass
        except 1:
         pass
        except 2:
         pass
        expect Exception as result
        
        else:
         #û쳣ŻִеĴ
         pass
        finally:
         #Ƿ쳣ִеĴ
         pass

        ʵ

        try:
         num = int(raw_input('һ'))
         result = 6 / num
         print result
        
        #δ֪쳣
        #resultһ
        except Exception as result:
         print 'δ֪ %s' % result
        #ֻû쳣ʱŻִеĴ
        else:
         print 'Գɹ'
        #Ƿ쳣ִеĴ
        finally:
         print '================'

        Ľ
        дͼƬ
        дͼƬ
        ȷĽ
        дͼƬ
        5.쳣-05

        쳣Ĵ
        쳣Ĵ--/ִг쳣Ὣ쳣ݸ/õһ
        ݵû쳣Żֹ
        쳣еֻҪ쳣ᴫݵ쳣
        ͲҪڴӴ쳣ܹ֤

        ʵ

        def demo1():
         return int(raw_input(':'))
        
        def demo2():
         return demo1()
        
        #쳣Ĵв쳣
        try:
         print demo2()
        except Exception as result:
         print 'δ֪ %s' % result

        Ľ
        дͼƬ
        ȷĽ
        дͼƬ
        6.쳣-06
        ʵ

        
        жû
        1.<8 
        2.>=8 
        def input_passwd():
         #1.ʾû
         pwd = raw_input('')
        
         #2.жij >-=8,ͷû
         if len(pwd) >= 8 :
          return pwd
         #3.<8,׳쳣
         print '׳쳣'
         #1.쳣(ӴϢ)
         ex = Exception('볤Ȳ')
         raise ex
        #עֻ׳쳣쳣
        try:
         print input_passwd()
        except Exception as result:
         print result

        ȷĽ
        дͼƬ
        Ľ
        дͼƬ

        test1,test2
        test1:

        title = 'ģ1'
        
        #
        def say_hello():
         print ' %s' % title
        
        #
        class Cat(object):
         pass

        test2:

        title = 'ģ2'
        
        #
        def say_hello():
         print ' %s' % title
        
        #
        class Dog(object):
         pass

        1.ģ-01

        __name__
        __name__ԿģڵĴֻڲ±ڱʱִ
         __name__pythonһ¼һַ
         DZļʱ__name__ģ
         ǵǰִеij__name___mian_

        ʵ

        # ȫֱ ֱִеĴ벻ṩĹ
        def say_hello():
         print 'helll hello'
        
        
        # print 'have a nice day!'
        # ֱģõʱ__main__
        if __name__ == '__main__':
         print __name__
        #ļʱֱܹӱִеĴ벻Ҫִ
         print 'gf ij'
         say_hello()

        ʵ
        дͼƬ
        2.ģ-02

        #ڵģʱÿӦռһ
        import test1
        import test2
        
        test1.say_hello()
        test2.say_hello()
        
        dog = test2.Dog()
        print dog
        
        cat = test1.Cat()
        print cat

        ʵ
        дͼƬ
        3.ģ-03

        #ʹasָģıշ
        import test1 as CatModule
        import test2 as DogModule
        
        DogModule.say_hello()
        CatModule.say_hello()
        
        dog = DogModule.Dog()
        print dog
        cat = CatModule.Cat()
        print cat

        дͼƬ
        4.ģ-04

        from test1 import  Cat
        from test2 import say_hello
        from test1 import say_hello as test1_say_hello
        say_hello()
        test1_say_hello()
        miaomiao = Cat()

        дͼƬ
        5.ģ-05

        pythonĽڵģʱ
         1.ǰĿ¼ָģļоֱӵ
         2.ûϵͳĿ¼
        עڿʱļҪϵͳģļ

        ʵ

        import random
        rand = random.randint(0,10)
        print rand

        дͼƬ
        6.ģ-06

        ΪǰԤ˸õ֪ԭ

        ʵ

        def func(num,p):
         assert (p != 0),'pΪ0'
         return num / p
        print func(10,2)

        дͼƬ
        7.ģ-07

        ںܶpythonļлῴ¸ʽĴ
         #ģ
         #ȫ
         #
         #庯
        
         #ڴ·
        
         def main():
          pass
        
         if __name__=='__mian__'
          mian()

        ʵ

        һtest
        def say_hello():
         print 'helll hello'
        print __name__
        #ڵ.pyļ ļ
        import test

        дͼƬ

        READMEREADME_COPYļ
        Ҽ => New => File
        1.ļ-01

        ļĺ/
        pythonҪļҪס13
         #pythonһнԶ
         open ļҷļ
         read ļݶȡڴ
         write ָдļ
         close رļ
        openļҷļ
        read/write/closeͨļ
        
        read--ȡļ
        openĵһҪ򿪵ļļִСд
         ļļ
         ļ׳쳣
        readһԶ벢ļ
        closeرļ

        ʵ

        #ǹرļϵͳһӰ쵽ļ
        #1.ļ
        file = open('README')
        
        #2.ļ /д
        #readȡļݣһԷļݣ
        text = file.read()
        print text
        
        #3.رļ
        #closeرļ
        file.close()
        
        #ڿͨȱд򿪺͹رյĴ

        ļREADME
        дͼƬ
        ʵ
        дͼƬ
        2.ļ-02

        ļָ
         ļָǴĸλÿʼȡ
         һδļʱͨļָָļĿʼλ
         ִreadļָƶȡݵĩβ
        #ǹرļϵͳһӰ쵽ļ
        #1.ļ
        file = open('README')
        
        #2.ļ /д
        #readȡļݣһԷļݣ
        text = file.read()
        print text
        
        #3.رļ
        #closeرļ
        file.close()
        
        #ڿͨȱд򿪺͹رյĴ

        ļREADME
        дͼƬ
        ʵ
        дͼƬ
        3.ļ-03

        #1.ļ
        file = open('README')
        
        #2.ļ /д
        #readȡļݣһԷļݣ
        text = file.read()
        print text
        
        #ӡݵij
        print type(text)
        print len(text)
        
        
        print '*' * 50
        text = file.read()
        print text
        print len(text)
        #3.رļ
        #closeرļ
        file.close()
        
        #ڿͨȱд򿪺͹رյĴ

        ļREADME
        дͼƬ
        ʵ
        дͼƬ
        4.ļ-04

        ļķʽ
         name = open('ļ','ʷʽ')

        ʵ

        #дķʽļļڻᱻļļ
        #1.ļ
        file = open('README','w')
        #2.дļ
        file.write('')
        #3.رļ
        file.close()

        ԭļREADME
        дͼƬ
        ʵ
        дͼƬ
        5.ļ-05

        # ׷ӷʽļ
        # ļļָļĩβ
        # ļļд
        # 1.ļ
        file = open('README','a')
        # 2.дļ
        file.write('linux')
        # 3.رļ
        file.close()

        ԭļREADME
        дͼƬ
        ʵ
        дͼƬ
        6.ļ-06

        жȡļ
          readĬϻļһԶڴ
          ļ̫ڴռûdz
        readline
          readlineһԶȡһ
          ִк,ļָƶһ׼ٴζȡ
        

        ʵ

        # ȡļȷ
        
        file = open('README')
        # ΪʲôҪдѭΪDz֪Ҫȡļж
        while True:
         text = file.readline()
         # ļָļһôͶ
         if not text:
          break
         # ÿȡһĩβѾһ\n
         print text
        file.close()

        ļREADME
        дͼƬ
        н
        дͼƬ
        7.ļ-07
        ʵ

        #1.ļ
        #Դļֻķʽ
        file_read = open('README')
        #Ŀļдķʽ
        file_write = open('README_COPY','w')
        
        #Դļжȡ
        text = file_read.read()
        #ȡдĿļ
        file_write.write(text)
        
        #رļ
        file_read.close()
        file_write.close()

        ļREADME
        дͼƬ
        ļREADME_COPY
        дͼƬ
        ʵ
        README_COPYļ
        дͼƬ
        8.ļ-08

        ȡļ

        ʵ

        # 1.ļ
        file_read = open('README')
        file_write = open('README_COPY', 'w')
        #д
        while True:
         text = file_read.readline()
         if not text:
          break
         file_write.write(text)
        #ر
        file_read.close()
        file_write.close()

        ļREADME
        дͼƬ
        ļREADME_COPY
        дͼƬ
        ʵ
        ļREADME_COPY
        дͼƬ

        ؼwithڲҪļر
        ǵopen(),ûеclose();ҲԵopen()close
        ͹رļʱbugclose()ûִ
        ļرδƵعرļܻᵼݶʧ
        ڳйصclose(),
        ᷢҪʹļʱѾر(޷)
        ᵼ¸ش㲢κ¶ɵȷرļǡʱ
        ͨʹwithṹpythonȥȷ
        ֻܴļҪʱʹ
        pythonںʵʱԶر

        linuxpi_digitsļ
        1.ļ-11

        with open('pi_digits') as file_object:
         contents = file_object.read()
         print contents

        ļpi_digits
        дͼƬ
        н
        дͼƬ
        2.ļ-12

        filename = 'pi_digits'
        with open(filename) as file_object:
         for line in file_object:
          print line

        ļpi_digits
        дͼƬ
        н
        дͼƬ
        3.ļ-13

        filename = 'pi_digits'
        with open(filename) as file_object:
         lines = file_object.readline()
        for line in lines:
         print line

        ļpi_digits:
        дͼƬ
        н:
        дͼƬ
        4.ļ-14

        filename = 'linux'
        with open(filename,'w') as file_object:
         file_object.write('I love python.\n')
         file_object.write('I love linux.')

        ļlinux
        дͼƬ
        н
        дͼƬ
        5.ļ-15

        filename = 'linux'
        with open(filename,'a') as file_object:
         file_object.write('I love python.\n')
         file_object.write('I love linux.')

        ļlinux
        дͼƬ
        н
        дͼƬ

        .ݴ-json

        ܶҪûijϢ
        򶼰ûṩϢбֵݽṹ
        ûرճʱ㼸ҪǵϢ
        һּ򵥵ķʽʹģjson
        pythonʹjsonʱҪҲjsonģjsonһõĸʽݽ
        ģjsonܹ򵥵Pythonݽṹת浽ļ
        ڳٴʱظļе
        㻹ʹjsonPython֮
        ҪjsonݽṹʽPythonרõ
        ܹjsonʽʹԵ˷
        
        עjson(javascriptObject notation)ʽΪjavascriptһֳʽ
        pythonڵڶԲ

        ʵһ

        import json
        number = [1,2,3,4]
        
        with open('number.json','w') as f_obj:
         # ʹújson.dump()б浽ļ
         json.dump(number,f_obj)

        н
        һļnumber.json
        дͼƬ
        ʵ

        import json
        filename = 'number.json'
        with open(filename) as f_obj:
         #ʹújson.loadشnumber.jsonϢ
         #䴢浽number
         number = json.load(f_obj)
        print number

        н
        дͼƬ
        ʵ

        import json
        username = raw_input('what is your name?')
        filename = 'username.json'
        with open(filename,'w') as f_obj:
         json.dump(username,f_obj)
         print 'We will remember you when you come back,%s' % username

        н
        ᴴһļusername.json
        дͼƬ
        дͼƬ
        ʵ
        ǰusername.json Ҵ

        import json
        filename = "username.json"
        with open(filename) as f_obj:
         username = json.load(f_obj)
         print 'Welcome back, %s' % username

        дͼƬ
        ʵ
        ǰusername.json

        
        ǰûʾ
        ʾûû
        import json
        filename = "username.json"
        try:
         with open(filename) as f_obj:
          username = json.load(f_obj)
        except ValueError:
         username = raw_input("What is you name? ")
         with open(filename,"w") as f_obj:
          json.dump(username,f_obj)
          print "We'll remember you when you come back %s" % username
        #tryɹִеĴ붼Ӧŵelse:
        else:
         print "Welcome back %s " % username

        н
        username.json
        дͼƬ
        username.json
        дͼƬ

        ѷ!رվҳ
        һƪpython Set Matrix Zeroes
        һƪpythonʹpandasʵ̳
        ͼƼ

        | ϵ | | Ͷʺ | Ȩ | ߰ | վͼ | Ʒ | Vipѵ | ٱ

        Ȩ: һŻ--ʵõITѧϰվ