Learn about Python and get a clean look at the basics of Python. seems to wait for 100 of years to understand, even if meet again, mature performance, not to be seen.
Some applications of Python one, similar to the Messageformat usage in Java
Word ="{0} love {1}"wordList= ('Huhx','Linux')Print(Word.format (*wordList))Print(Word.format ('Huhx','Linux')) Str_word="{name} love {lover}"Wordmap= Dict (name='Huhx', lover='Linux')Print(Str_word.format (name='Huhx', lover='Linux'))Print(Str_word.format (* *wordmap))Print(Str_word.format_map (Wordmap))
The print results are: Huhx Love Linux
Second, the understanding of greedy matching in the regular
ImportRedata='Thu Feb 17:46:04 2007::[email protected]::1171590364-6-8'Print(Re.search ('\d+-\d+-\d+', data). Group ())#1171590364-6-8Print(Re.match ('. +\d+-\d+-\d+', data). Group ())#Thu Feb 17:46:04 2007::[email protected]::1171590364-6-8Print(Re.match ('. + (\d+-\d+-\d+)', data). Group (1))#4-6-8Print(Re.match ('.+? (\d+-\d+-\d+)', data). Group (1))#1171590364-6-8
C. Simple JSON request in Python
Importhttp.clientImportJsonconn= Http.client.HTTPConnection ("hostname") Data= { 'Tellerid':'***', 'Password':'***'}payload=json.dumps (data) headers= { 'Content-type':"Application/json"}conn.request ("POST","/mweb/login.do", payload, headers) Res=conn.getresponse () data=Res.read ()Print(Data.decode ("Utf-8"))
Iv. simple MySQL connection in Python
You can first install Pymysql by: Pip install pymysql.
ImportPymysqldb= Pymysql.connect ("localhost","Root","Pass","Database", charset='UTF8') cur=db.cursor () Cur.execute ('SELECT * from Puser') forIinchCur.fetchall ():Print(str (i)) db.close ()
V. Simple use of the SFTP connection used in Python
First available via: Pip install Paramiko installation of SFTP support
ImportParamikohost='hostname'Port= 22User='Root'passwd='Password'#To create an SSH objectSSH =Paramiko. Sshclient ()#allow connections to hosts that are not in the Know_hosts fileSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#connecting to a serverSsh.connect (Hostname=host, Port=port, Username=user, password=passwd)#Execute Commandstdin, stdout, stderr = Ssh.exec_command ('Ls-al')#Get command Resultsresult =Stdout.read ()Print(Result)#Close ConnectionSsh.close ()
Six, Python simple connection operation Redis
Start the Redis service first, and then download the Python Redis package: Pip install Redis.
Import= Redis. Redis (host='127.0.0.1', port=6379) r.set ('name' ' Linux ' )print(r.get ('name'# Linux
Vii. python simple DOM parsing of XML
fromXml.dom.minidomImportParseImportXml.dom.minidomDOMTree= Xml.dom.minidom.parse ("Huhx.xml") Collection=domtree.documentelementpersons= Collection.getelementsbytagname (' Person') forPersoninchpersons:ifPerson.hasattribute ('name'): Print('My name is%s:'% Person.getattribute ('name')) age= Person.getelementsbytagname (' Age') [0].childnodes[0].data address= Person.getelementsbytagname ('Address') [0].childnodes[0].dataPrint('Age =%d, address =%s.'% (int (age), address))
The test Huhx.xml file is as follows:
<Persons> < Personname= "Huhx"> < Age>12</ Age> <Address>Wuhan</Address> </ Person> < Personname= "Linux"> < Age>1</ Age> <Address>Hubai</Address> </ Person></Persons>
The results of the operation are as follows:
is:address =is1, address = Hubai.
Viii. reading and writing of CSV files in Python
A CSV file can be used to create data in an Excel table and save it as a CSV file in the last saved format.
- First read the contents of a CSV file, and test the contents of the Huhx.csv file as follows:
Import= open ('huhx.csv'r'= Csv.reader (csvfile) for in reader: for in Item: Print(subitem, end="") print()
The results of the operation are as follows:
all
- Python writes content to a CSV file
ImportCsvfileheader= ['username',' Age','Address']data1= ['Huhx', 342,'Hubai']data2= ['Linux', 56,'Wuhan']data3= ['Tomhu', 44,' China']csvfile= Open ('Huhx.csv','a') Writer=Csv.writer (csvfile)#Writer.writerow (Fileheader)#Writer.writerow (data1)#Writer.writerow (data2)#Writer.writerow (DATA3)writer.writerows ([Fileheader, Data1, Data2, data3]) csvfile.close ()
Friendship Link
Python Basics---->python use (ii)