当前位置: 移动技术网 > IT编程>脚本编程>Python > python 3 在工作中的应用

python 3 在工作中的应用

2019年05月28日  | 移动技术网IT编程  | 我要评论

新手卡365之家,侯卫东官场全集,健康九州网赶牛阿旗

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包

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

 

python 3 日志记录

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

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网