首页 > 技术文章 > python学习笔记

onegarden 2017-04-07 21:00 原文

Python学习笔记,主要是用python做一些琐碎的事情,python版本为3.5

  • 读写txt
  • 读写excel
  • 读写xml
  • 访问数据库

 

利用python的xkrd和xlwt读写excel

测试xls和xlsx都是可以打开的,使用xlrd读取excel文件不是以独占的方式读取的,读取的时候excel仍然可以打开。写excel只能写xls

import xlrd
import xlwt
import xml
from xml.dom import minidom
import os


#简单的IO操作保存txt
def createtxt():
    ls=os.linesep
    #get filename
    while True:
        fname=input('Enter filename:')
        if os.path.exists(fname):
            print("error:'%s' already exists"%fname)
        else:
             break;

    #get file content(text)lines
    all=[]
    print("\nEnter lines('.'by itself to quit).\n")

    #loop until user terminates input
    while True:
        entry=input('>')
        if entry=='.':
            break
        else:
            all.append(entry)

    #write ines to file with proper line-ending

    fobj=open(fname,'w')
    fobj.writelines(['%s%s'%(x,ls) for x in all])
    fobj.close()
    print('DONE!')
    f=open(fname,'r')
    for l in f.readlines():
        print(l,end='')#这个地方多出一个空行应该是windows下bom的原因
    

#读写excel
def rwexcel():
    path=r'C:\Users\huach\Desktop\右岸交通洞-渗压计.xlsx'
    file=xlrd.open_workbook(path)#打开文件
    sheet=file.sheet_by_index(0)#根据索引获取第一个sheet
    #sheet=file.sheet_by_name('Pe1-1')
    nc=sheet.ncols#获取sheet的列数
    nr=sheet.nrows#sheet 行数

    newbook=xlwt.Workbook()#新建一个excel对象
    nsheet=newbook.add_sheet('testsheet',cell_overwrite_ok=True)#添加一个sheet
    for i in range(20):
        row=sheet.row_values(i)#获取第i行,这是个数组
        for j in range(nc):
            print(row[j],end='')
            nsheet.write(i,j,row[j])#写到新的sheet里边
        print()
    newfile=r'C:\Users\huach\Desktop\test.xls'
    if os.path.exists(newfile):os.remove(newfile)#有就删除了
    newbook.save(newfile)#把新创建的excel对象保存

#读写xml
def xml():
    #import xml.dom.minidom
    impl = minidom.getDOMImplementation()#创建xml对象
    dom = impl.createDocument(None, 'Root', None)  #创建dom
    root = dom.documentElement#根节点
    root.setAttribute('attr','root')#属性
    for i in range(1,3):
        employee=dom.createElement('employee')#添加子节点
        employee.setAttribute('attr','child')#添加属性
        value=dom.createTextNode('value')
        employee.appendChild(value)#添加值
        
        root.appendChild(employee)
    #保存
    file=r'C:\Users\huach\Desktop\test.xml'
    if os.path.exists(file):os.remove(file)#有就删除了
    f=open(file,'a')
    dom.writexml(f, addindent='  ', newl='\n')
    f.close()
    print('xml写入成功')
    #读取
    dom = minidom.parse(file)
    root = dom.documentElement
    ems=root.getElementsByTagName('employee')
    for em in ems:
        attr=em.getAttribute("attr")#属性
        print('em.attr=%s'%attr)
        print('em.attr=%s'%em.firstChild.data)#

#访问mssql,引用pymssql,这里是别人的简单封装,可以直接用,简单的查询增删查改没问题·谢谢原创,· import pymssql class MSSQL: """ 对pymssql的简单封装 pymssql库,该库到这里下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql 使用该库时,需要在Sql Server Configuration Manager里面将TCP/IP协议开启 用法: """ def __init__(self,host,user,pwd,db): self.host = host self.user = user self.pwd = pwd self.db = db def __GetConnect(self): """ 得到连接信息 返回: conn.cursor() """ if not self.db: raise(NameError,"没有设置数据库信息") self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8") cur = self.conn.cursor() if not cur: raise(NameError,"连接数据库失败") else: return cur def ExecQuery(self,sql): """ 执行查询语句 返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段 调用示例: ms = MSSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics") resList = ms.ExecQuery("SELECT id,NickName FROM WeiBoUser") for (id,NickName) in resList: print str(id),NickName """ cur = self.__GetConnect() cur.execute(sql) resList = cur.fetchall() #查询完毕后必须关闭连接 self.conn.close() return resList def ExecNonQuery(self,sql): """ 执行非查询语句 调用示例: cur = self.__GetConnect() cur.execute(sql) self.conn.commit() self.conn.close() """ cur = self.__GetConnect() cur.execute(sql) self.conn.commit() self.conn.close() def main(): ## ms = MSSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics") ## #返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段 ## ms.ExecNonQuery("insert into WeiBoUser values('2','3')") ms = MSSQL(host="localhost",user="sa",pwd="sa",db="MWDatabase") resList = ms.ExecQuery("select * from Fiducial_Anchor_Cable") for li in resList: print(li) #本地数据sqlite import sqlite3 def insert(): conn=sqlite3.connect('test.db') cursor=conn.cursor() cursor.execute('create table user(id varchar(20) primary key,name varchar(20))') cursor.execute('insert into user(id,name) values (\'1\',\'Michael\')') cursor.rowcount cursor.close conn.commit() conn.close() def select(): conn=sqlite3.connect('test.db') cursor=conn.cursor() cursor.execute('select * from user where id=?',('1',)) values=cursor.fetchall() print(values) cursor.close() conn.close() if __name__=='__main__': xml()

调用COM的方式读写excel 

上边是调用第三方的库,不确定第三方是怎么实现excel读写的,还可以采用调用com接口的方式读写excel,像其他语言一样调用com接口读写excel,这里需要安装pywin32模块,安装完以后需要把Lib\site-packages\pywin32_system32下的DLL复制到windows

系统下system32的目录下,不然总提示导入win32api失败。

这个win32api有点腻害的样子,好像可以通过这个调用很多win32api,比如下边messagebox,还可以直接挂载windows的消息以及发送消息,这里就不举例子了·

import win32api
import win32con
import win32com.client as com

def comTest():
    excel =com.Dispatch("Excel.Application")
    workbook=excel.Application.Workbooks.Add(True)
    sheet=workbook.Sheets(1);
    for r in range(20):
        for c in range(10):
            sheet.Cells(r+1,c+1).Value=r*c

    filepath=os.path.join(os.getcwd(),r'test.xlsx')
    if os.path.exists(filepath):os.remove(filepath)#有就删除了
    workbook.SaveAs(filepath)
    workbook.Close()
    excel.Quit()



if __name__=='__main__':
    #comTest()
    win32api.MessageBox(win32con.NULL, 'Python 你好!', '你好', win32con.MB_OK)  

这个com的写法就不局限于excel了,

可以调用所有实现了com接口的东西,写法类似C#的动态类型写法,只需要创建一个application对象,其他的都可以直接动态创建或获取,不过本身python也是动态类型语言,应该这么写

访问oracle

需要安装cx_Oracle库

具体代码如下:


import cx_Oracle
dbstr='usr/pwd@192.168.0.172/orcl'
db=cx_Oracle.connect(dbstr)

def
exc(sql,db): cr=db.cursor() cr.prepare(sql) x=cr.execute(sql) queryList=[] for item in x: queryList.append(list(item)) cr.close() return queryList def edit(sql,db): cr=db.cursor() cr.prepare(sql) cr.execute(sql) res=db.commit() cr.close() return res

一个是执行查询,一个是执行insert delete update操作

 

推荐阅读