Python Module Summary Exercise

Source: Internet
Author: User
Tags serialization

1. Random Module
# print (Random.random ()) # Print (Random.randint (1,3)) # analog Random Verification code def Make_code (n=5):    res=' for in '      Range (n):        S1=str (random.randint (0,9))        S2=chr (Random.randint (65,90))        res+ =  Random.choice ([s1,s2])    return  resprint(Make_code (10))

2. Simulation Printing progress bar
1Num=302 Print('%s%%'%num)3 4 #Print progress bar5Width=506 Print(('[%%-%ds]'%width)%'#')7 Print(('[%%-%ds]'%width)%'##')8 Print(('[%%-%ds]'%width)%'###')9 Print(('[%%-%ds]'%width)%'####')Ten  One defProgress (percent,width=50): A     ifPercent > 1: -Percent=1 -Show_str= (('[%%-%ds]'%width)% (int (width*percent) *'#')) the     Print('\r%s%d%%'% (Show_str,int (100*percent)), end=' ') -  - ImportTime,random -Recv_size=0 +total_size=8097 -  whilerecv_size<total_size: +Time.sleep (0.5) Arecv_size+=1024 atPercent=recv_size/total_size -Progress (percent)

3. Shelve Module (serialization)
1 Importshelve2 3 #Serialization of4info1={' Age': 18,'Herght': 180,'Weight': 80}5info2={' Age': 88,'Herght': 150,'Weight': 80}6 7D=shelve.open ('DB.SHV')8d['Egon']=Info19d['Alex']=Info2Ten d.close () One  A #deserialization -D=shelve.open ('DB.SHV') - Print(d['Alex']) the Print(d['Egon']) - d.close () -  -  + #Modifying Data -D=shelve.open ('DB.SHV', writeback=True) + #d[' Alex ' [' Age ']=10000 A Print(d['Alex']) atD.close ()

4. XML module
1 ImportXml.etree.ElementTree as ET2Tree=et.parse ('A.xml')3root=tree.getroot ()4 5 #======================================== ""6 #three ways to find nodes7Res=root.iter ('Rank')#is searched throughout the tree, and it is the search for all8  forIteminchRes:9     #print (item)Ten     Print('='*50) One     Print(Item.tag)#Label name A     Print(Item.attrib)#Properties -     Print(Item.text)#text content -  the  -Res=root.find ('Country')#can only be found at the next level of the current element, and only one - #print (Res.tag) - #print (Res.attrib) + #print (Res.text) -N=res.find ('Rank') + Print(N.attrib) A  at  -  -Res=root.findall ('Country')#can only be found at the next level of the current element, - Print([Item.tag forIteminchRes]) -  -  in  - #===================================== "" Changed to ImportXml.etree.ElementTree as ET +Tree=et.parse ('A.xml') -root=tree.getroot () the  *  forYearinchRoot.iter (' Year'): $YEAR.TEXT=STR (int (year.text) +10)Panax Notoginsengyear.attrib={'Updated':'Yes'} -Tree.write ('A.xml') the  +  A  the #=============================== "Increased + ImportXml.etree.ElementTree as ET -Tree=et.parse ('A.xml') $root=tree.getroot () $  -  forCountryinchRoot.iter ('Country'): -     #print (' year ') theYear=country.find (' Year') -     #print (year)Wuyi     ifInt (year.text) > 2010: the         #print (Country.attrib) -         #Ele=et. Element (' Egon ') Wu         #ele.attrib={' nb ': ' Yes ' -         #ele.text= ' good ' About         #country.append (ele) $ Country.remove (year) -  -Tree.write ('B.xml')

2018-08-15 22:54:40

Python Module Summary Exercise

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.