实践了一把python操作excel,以及cx_Oracle操作oracle,并读取excel表格数据到oracle。
开发环境
windows10 python3.5
excel操作练习
# encoding: utf-8
import xlrd
# 设置GBK编码
xlrd.Book.encoding = "gbk"
excelPath = r"C:\Users\think\Desktop\盘锦项目\盘锦三期\视频集成相关\视频导入模板.xls"
#打开Excel文件读取数据
data = xlrd.open_workbook(excelPath)
#获取一个工作表
#table = data.sheets()[0]
#通过索引顺序获取
#table = data.sheet_by_index(0) #通过索引顺序获取
table = data.sheet_by_name(u'Sheet1')#通过名称获取
#获取整行和整列的值(数组)
print(table.row_values(1))
print(table.col_values(1))
#获取行数和列数
nrows = table.nrows
ncols = table.ncols
#循环行列表数据
for i in range(1,nrows ):
print(table.row_values(i))
#单元格
cell_A1 = table.cell(0,0).value
cell_C4 = table.cell(1,2).value
#使用行列索引
cell_A1 = table.row(0)[0].value
cell_A2 = table.col(1)[0].value
#简单的写入
row = 0
col = 0
# 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
ctype = 1
value = "单元格的值"
xf = 0 # 扩展的格式化
table.put_cell(row, col, ctype, value, xf)
table.cell(0,0) #单元格的值'
table.cell(0,0).value #单元格的值'
oracle操作练习
import cx_Oracle
#参数:用户名/密码@监听(server主机:server端口/server名称)
#conn = cx_Oracle.connect("test/root@10.80.17.5:1521/userTest")
#参数为:用户名、密码、监听(server主机:server端口/server名称)
#conn = cx_Oracle.connect("test", "root", "10.80.17.5:1521/userTest")
#参数为:用户名、密码、TSN
conn = cx_Oracle.connect("tyum_um", "DEV_TYUM_UM", "PJDB_215")
#创建cursor
cursor = conn.cursor()
sql = "select * from t_um_ob"
#执行sql语句
cursor.execute(sql)
#获取所有的记录
one = cursor.fetchall()
#一条一条的访问记录
for x in one:
print(x)
#关闭cursor
cursor.close()
#关闭连接
conn.close()
读取excel导入到oracle
需求:读取excel数据并更新回oracle,excel表格数据见下图:
参考代码如下,做了简单的错误处理:
# encoding: utf-8
import xlrd
import cx_Oracle
import re
####配置项######
# 设置excel GBK编码
xlrd.Book.encoding = "gbk"
#Oracle连接配置:用户名、密码、TSN
conn = cx_Oracle.connect("ty_zhzf", "DEV_TY_ZHZF", "PJDB_215")
cursor = conn.cursor()
#excel路径
excelPath = r"C:\Users\think\Desktop\盘锦项目\盘锦三期\视频集成相关\宜居乡村视频点位2.0.xlsx"
class Camera(object):
pass
def getFloatValue(value , default=0.0):
if( value == None or len(str(value))==0):
return default
else:
return float(value)
def run():
#打开Excel文件读取数据
data = xlrd.open_workbook(excelPath)
table = data.sheets()[0]
#获取行数和列数
nrows = table.nrows
ncols = table.ncols
sql = """update t_sp_video set c_x=:c_x,c_y=:c_y where c_camera_name =:c_camera_name"""
colName=""
cameraFullName=""
cameraName=""
lon=0.0
lat=0.0
cellRowNum=-1
lstCameraName=[]
camera=None
lstCamera =[]
lstFails =[]
#遍历excel表
if(nrows>0):
#循环行列表数据,从第1行开始;
for i in range(0,nrows ):
try:
colName = table.row_values(i)[0]
if(colName =="点位名称") :
cameraFullName = table.row_values(i)[1]
regex = re.search('(?<=[村| 会])[\s|\S]*?(?=[\(|\(])',str(cameraFullName))
if(regex == None):
#cameraName = cameraFullName[-11:-7]
cameraName =""
print("第 {0} 行解析摄像头名称失败!原文:{1}".format(i+1,str(cameraFullName)))
cameraFullName=""
continue
else :
cameraName = regex.group()
lstCameraName.append(cameraName)
cellRowNum = i+1
continue
if(colName =="纬度") :
lat = getFloatValue(table.row_values(i)[1])
continue
if(colName =="经度") :
lon = getFloatValue(table.row_values(i)[1])
if(lat>0 and lon >0 and len(cameraName)>0) :
camera = Camera()
camera.cameraName = cameraName
camera.lat=lat
camera.lon=lon
camera.fullName=cameraFullName
camera.cellRowNum=cellRowNum
lstCamera.append(camera)
except Exception as ex:
print("第 {0} 行解析摄像头名称失败!,失败原因:{1}".format(i+1,str(ex)))
lon=-0.0
lat=-0.0
# #查找重复的
# duplicates = set([x for x in lstCamera if lstCameraName.count(x.cameraName)>1]) #count()=>total number of occurrences of x in s
# for item in duplicates :
# print(item)
for item in lstCamera :
if(lstCameraName.count(item.cameraName)>1):
lstCamera.remove(item)
print("ERROR! 第 {0} 行摄像头名称有重复!原文:{1}".format(item.cellRowNum,item.fullName))
lstFails.append(item.cellRowNum)
else :
cursor.execute(sql,{
'c_x' : item.lon,
'c_y': item.lat,
'c_camera_name' : item.cameraName
})
rowCount = cursor.rowcount
if(rowCount==1):
# print("第 {0} 行导入成功!原文:{1} 受影响行数 {2}".format(item.cellRowNum,item.fullName,rowCount))
pass
elif (rowCount >1) :
print("ERROR! 第 {0} 行导入有重复!原文:{1} 受影响行数 {2}".format(item.cellRowNum,item.fullName,rowCount))
lstFails.append(item.cellRowNum)
elif (rowCount <=0) :
print("第 {0} 行导入失败!原文:{1}".format(item.cellRowNum,item.fullName))
lstFails.append(item.cellRowNum)
conn.commit()
cursor.close()
conn.close()
print(lstFails)
print(len(lstFails))
run()
总体感觉比写强类型开发语言要爽一些,和写JS一样,爽得一B。