新手卡365之家,侯卫东官场全集,健康九州网赶牛阿旗
python 3在工作中的使用
安装
pip 命令
在notepad++的程序根目录下,编辑shortcuts.xml文件。在 userdefinedcommands节点下输入:
<command name="python 3" ctrl="no" alt="no" shift="no" key="0">cmd /k python $(full_current_path)</command>
然后,编写并保存python程序*.py,通过点击菜单上的"运行">"python 3"即可通过python执行程序。
另外,如果需要使用快捷键启动,也可以在上面的xml中设置或通过菜单设置。
连接sql server数据库
由于pymssql暂时不支持python3,无法使用;发现可以通过pyodbc连接sql server数据库。
访问数据库
1 import pyodbc 2 conn = pyodbc.connect('driver={sql server};server=gcdc-sqltest01;database=gconline;uid=isystem;pwd=isystem') 3 cur = conn.cursor() 4 cur.execute("select top 100 * from agent") 5 row = cur.fetchone() 6 row[0]
相关的包:
读取excel - xlrd包
写入excel - xlwt包
参考:
1 import xlwt 2 new_workbook = xlwt.workbook() 3 new_sheet=new_workbook.add_sheet("pysheet1") 4 new_sheet.write(0,0,"hello") 5 new_sheet.write(2,0,5) 6 new_sheet.write(2,1,8) 7 new_sheet.write(3,0,xlwt.formula("a3+b3")) 8 new_workbook.save(r"d:\pycreateworkbook.xls")
d盘下excel文件结果
a |
b |
c |
... |
|
1 |
hello |
|||
2 |
||||
3 |
5 |
8 |
||
4 |
13 |
发送email (email.mycompany.com)
(带附件)
发送一般文本邮件
1 import smtplib 2 from email.mime.multipart import mimemultipart 3 msg=mimemultipart() 4 msg['subject']='this is the email\'s subject' 5 msg['from']='peter@mycompany.com' 6 msg['to']='peter@mycompany.com;alice@mycompany.com' 7 s=smtplib.smtp('mail.mycompany.com') 8 s.send_message(msg) #触发发送邮件动作 9 s.quit()
另外,yagmail包发送邮件很方便,但是很遗憾exchange暂时无法使用。
发送html格式邮件
1 import smtplib 2 from email.mime.text import mimetext 3 content_msg = ''' 4 <p>这是一封<strong>html</strong>文本邮件</p> 5 <a href="https://wx.qq.com/" title="点击打开">微信网页版</a> 6 ''' 7 msg=mimetext(content_msg,'html','utf-8') 8 msg['subject']='this is the email\'s subject' 9 msg['from']='peter@mycompany.com' 10 msg['to']='peter@mycompany.com;alice@mycompany.com' 11 s=smtplib.smtp('mail.mycompany.com') 12 s.send_message(msg) #触发发送邮件动作 13 s.quit()
发送带附件的邮件
1 import smtplib 2 from email.mime.text import mimetext 3 from email.mime.multipart import mimemultipart 4 msg=mimemultipart() 5 msg['from']='peter@mycompany.com' 6 msg['to']='peter@mycompany.com;alice@mycompany.com' 7 msg['subject']='通过python 3发送的测试邮件' 8 msg.attach(mimetext('这是一封测试邮件,请忽略','plain','utf-8')) 9 att1 = mimetext(open('d:\\pycreateworkbook.xls','rb').read(),'base64','utf-8') 10 att1["content-type"]='application/octet-stream' 11 att1["content-disposition"]='attachment;filename="bj.xls"' 12 msg.attach(att1) 13 s=smtplib.smtp('mail.mycompany.com') 14 s.send_message(msg) #触发发送邮件动作 15 s.quit()
https://www.cnblogs.com/devopser/p/6366975.html
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论