Python Basics---->python Use (iii)

Source: Internet
Author: User

Today is 2017-05-03, here are some basic methods of using Python. There is no sorrow in the world that cannot be shed, and this sorrow cannot be explained to others, even if it is explained. It is always static, such as no wind at night snowflakes quietly deposited in the bottom of my heart.

Python Basics Learning One, Python functions as parameters
Import Math def Add (x, Y, f):     return f (x) + f (y)print#  8.0

Ii. python referencing a custom module

Define two files huhx.py and huhx5.py under the same directory. Where huhx.py is used as a module for huhx5.py.

    • The contents of the huhx.py file are as follows:
# defined a module " Linux " def saymyname ():     Print ("", username)
    • The contents of the huhx5.py file are as follows:
Import huhxhuhx.saymyname ();
    • The printing effect is as follows:

Iii. reading and writing of Python files
#write the file, which is generally used to write shorter charactersWith open ('.. /file/huhx.xml','W', encoding='Utf-8') as F:f.write ('Hello, huhx!.')
#The read of the file, which is typically used to read short charactersWith open ('.. /file/huhx.xml','R', encoding='Utf-8') as F:Print(F.read ())

Iv. stmtp simple Email in python
ImportSmtplib fromEmail.headerImportHeader fromEmail.mime.textImportMimetext fromEmail.utilsImportparseaddr, Formataddrdef_format_addr (s): Name, addr=parseaddr (s)returnFormataddr (Header (name,'Utf-8'). Encode (), addr) from_addr='****'Password='*****'to_addr='******'Smtp_server='smtpcom.263xmail.com'msg= Mimetext ('Hello, my name is HUHX.','Plain','Utf-8') msg[' from'] = _FORMAT_ADDR ('HUHX <%s>'%from_addr) msg[' to'] = _FORMAT_ADDR ('Linux <%s>'%to_addr) msg['Subject'] = Header ('Subject Info','Utf-8'). Encode () server= Smtplib. SMTP (Smtp_server, 465) Server.set_debuglevel (1) server.login (from_addr, password) server.sendmail (from_addr, [to_addr], msg.as_string ()) Server.quit ()

If you are sending an HTML message, change the above msg to the following:

msg = Mimetext ('' +               ' ) <p>send by <a href= "http://www.python.org" >Python</a>...</p> ' +               '</body>'html'  'utf-8')

If you need to send an attachment:

ImportSmtplib fromEmailImportencoders fromEmail.headerImportHeader fromEmail.mime.baseImportMimebase fromEmail.mime.multipartImportMimemultipart fromEmail.mime.textImportMimetext fromEmail.utilsImportparseaddr, Formataddrdef_format_addr (s): Name, addr=parseaddr (s)returnFormataddr (Header (name,'Utf-8'). Encode (), addr) from_addr='****'Password='*****'to_addr=' * * *@qq. com'Smtp_server='smtpcom.263xmail.com'#Mail object:msg =Mimemultipart () msg[' from'] = _FORMAT_ADDR ('Hello <%s>'%from_addr) msg[' to'] = _FORMAT_ADDR ('HUHX <%s>'%to_addr) msg['Subject'] = Header ('Subject Info','Utf-8'). Encode ()#the message body is Mimetext:Msg.attach (Mimetext ('send with File ...','Plain','Utf-8'))#Adding an attachment is to add a mimebase that reads a picture from the Local:With open ('Huhx.png','RB') as F:#set the MIME and file name of the attachment, here is the PNG type:MIME = Mimebase ('Image','PNG', filename='Test.png')    #Add the necessary header information:Mime.add_header ('content-disposition','Attachment', filename='Test.png') Mime.add_header ('Content-id','<0>') Mime.add_header ('X-attachment-id','0')    #read the contents of the attachment in:Mime.set_payload (F.read ())#code with BASE64:Encoders.encode_base64 (MIME)#Add to Mimemultipart:Msg.attach (MIME) server= Smtplib. SMTP (Smtp_server, 465) Server.set_debuglevel (1) server.login (from_addr, password) server.sendmail (from_addr, [to_addr], msg.as_string ()) Server.quit ()

V. Simple HTML parsing in Python
 fromHtml.parserImportHtmlparserclassMyhtmlparser (htmlparser):defHandle_starttag (self, Tag, attrs):Print("encountered a start tag:", Tag)defHandle_endtag (self, tag):Print("encountered an end tag:", Tag)defHandle_data (self, data):Print("encountered some data:", data) parser=Myhtmlparser () parser.feed (''            '<body>')

The results of the operation are as follows

encountered a start tag:htmlencountered a start tag:headencountered a start tag:titleencountered some data  : Te Stencountered an end tag:titleencountered an end tag:headencountered a start tag:bodyencountered a start Tag:h1encou ntered some data  : Parse me! encountered an end tag:h1encountered a end tag:bodyencountered an end tag:html

Friendship Link

Python Basics---->python Use (iii)

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.