今天是2017-05-03,这里记录一些python的基础使用方法。世上存在着不能流泪的悲哀,这种悲哀无法向人解释,即使解释人家也不会理解。它永远一成不变,如无风夜晚的雪花静静沉积在心底。
Python的基础学习
一、python中函数作为参数
import math def add(x, y, f): return f(x) + f(y) print(add(25, 9, math.sqrt)) # 8.0
二、python引用自定义模块
在同目录下面定义两个文件huhx.py和huhx5.py。其中huhx.py作为一个模块供huhx5.py使用。
- huhx.py文件内容如下:
# defined a module username = "Linux" def sayMyName(): print("My name is ", username)
- huhx5.py文件内容如下:
import huhx huhx.sayMyName();
- 打印效果如下:
三、python文件的读写
# 文件的写,这一般用于写比较短的字符 with open(‘../file/huhx.xml‘, ‘w‘, encoding=‘utf-8‘) as f: f.write(‘您好, huhx!‘) # 文件的读,这一般用于读取比较短的字符 with open(‘../file/huhx.xml‘, ‘r‘, encoding=‘utf-8‘) as f: print(f.read())
四、python中stmtp简单发送邮箱
import smtplib from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr def _format_addr(s): name, addr = parseaddr(s) return formataddr((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()
如果发送的是html邮件,则将上述的msg改为如下:
msg = MIMEText(‘<html><body><h1>Hello</h1>‘ + ‘<p>send by <a href="http://www.python.org">Python</a>...</p>‘ + ‘</body></html>‘, ‘html‘, ‘utf-8‘)
如果需要发送附件:
import smtplib from email import encoders from email.header import Header from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import parseaddr, formataddr def _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, ‘utf-8‘).encode(), addr)) from_addr = ‘****‘ password = ‘*****‘ to_addr = ‘***@qq.com‘ smtp_server = ‘smtpcom.263xmail.com‘ # 邮件对象: 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() # 邮件正文是MIMEText: msg.attach(MIMEText(‘send with file...‘, ‘plain‘, ‘utf-8‘)) # 添加附件就是加上一个MIMEBase,从本地读取一个图片: with open(‘huhx.png‘, ‘rb‘) as f: # 设置附件的MIME和文件名,这里是png类型: mime = MIMEBase(‘image‘, ‘png‘, filename=‘test.png‘) # 加上必要的头信息: mime.add_header(‘Content-Disposition‘, ‘attachment‘, filename=‘test.png‘) mime.add_header(‘Content-ID‘, ‘<0>‘) mime.add_header(‘X-Attachment-Id‘, ‘0‘) # 把附件的内容读进来: mime.set_payload(f.read()) # 用Base64编码: encoders.encode_base64(mime) # 添加到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()
五、python中简单的html解析
from html.parser import HTMLParser class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Encountered a start tag:", tag) def handle_endtag(self, tag): print("Encountered an end tag :", tag) def handle_data(self, data): print("Encountered some data :", data) parser = MyHTMLParser() parser.feed(‘<html><head><title>Test</title></head>‘ ‘<body><h1>Parse me!</h1></body></html>‘)
运行的结果如下
Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html
友情链接
时间: 2024-10-08 02:05:03