Python 3在工作中的使用


 

安装配置Python 3

安装

  1. 首先确保在python36的Script文件夹路径下执行命令。或者,最好将Windows环境变量设置为python.exe所在路径和pip所在路径。
  2. python> pip install pyodbc

pip 命令

  • pip install package-name #安装软件包
  • pip list # 显示pip安装的软件包列表
  • pip show package-name # 显示软件包的信息

在notepad++中配置Python 3

在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数据库

连接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
  • xlwt
  • xlutils

读取Excel - xlrd包

https://www.cnblogs.com/miniren/p/5763931.html

写入Excel - xlwt包

参考:https://www.cnblogs.com/miniren/p/5763931.html

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)

https://www.cnblogs.com/vivivi/p/5952093.html

http://blog.csdn.net/u013511642/article/details/44251799 (带附件)

http://www.runoob.com/python3/python3-smtp.html

发送一般文本邮件

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()

 

Python 3 日志记录

https://www.cnblogs.com/Devopser/p/6366975.html

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!