import sys
from orator import DatabaseManager
import xlrd dbconfig = {
'mysql': {
'driver': 'mysql',
'host': "",
'database': "",
'user': "",
'password': "",
'port': 3306
}
}
db = DatabaseManager(dbconfig) def loadConfig():
return {
"file": ["f:\\11.xlsx"],
"startRow": 5,
"table": "yy_oph2",
"cleanBeforeImport": 1,
"comitcount": 100,
"map": {
"r": "ophno"
},
"defaultValueMap": {
"userid": 1
}
} def chang26to10(value):
s = value.upper()
re = 0
for x in s:
re *= 26
re += ord(x) - ord('A') + 1
return re if __name__ == '__main__':
importConfig = loadConfig()
# 获取要导入的表名
tableName = importConfig["table"]
comitcount = importConfig["comitcount"]
startRow = importConfig["startRow"]
cleanBeforeImport = importConfig["cleanBeforeImport"]
defaultValueMap = importConfig["defaultValueMap"]
if cleanBeforeImport == 1:
print("确定要清空%s表数据请输入yes?" % (tableName))
flag = sys.stdin.readline().strip()
if flag.upper() == "YES":
db.table(tableName).truncate()
kv = importConfig["map"]
defaultkv = importConfig["defaultValueMap"]
# 支持多文件导入
for fileidx in range(len(importConfig["file"])):
xls = xlrd.open_workbook(importConfig["file"][fileidx])
sheetcount = len(xls.sheets())
# 多sheet导入
for sidx in range(sheetcount):
sheet = xls.sheets()[sidx]
startIdx = startRow
vlist = []
for i in range(startIdx - 1, sheet.nrows):
print("处理第%s行" % i)
dict = {}
for k, v in kv.items():
dict[v] = str(sheet.cell_value(i, chang26to10(k) - 1)).replace("'", "''")
for k, v in defaultkv.items():
dict[k] = v
vlist.append(dict)
# 每50行提交一次
if len(vlist) == comitcount:
db.table(tableName).insert(vlist)
vlist.clear()
continue
if len(vlist) > 0:
db.table(tableName).insert(vlist)
vlist.clear()
print("处理完成")

最新文章

  1. 《HiWind企业快速开发框架实战》(3)使用HiWind创建和管理菜单
  2. Server 对象
  3. 详解Android首选项框架ListPreference
  4. Demo学习: Dialogs Anonymous Callback
  5. TextBox控件
  6. [Jquery] jQuery.cookie帮助类 (转载)
  7. 转:C++中 #ifdef 和#endif的作用
  8. aix 小机运维
  9. sql防注入代码
  10. delphi 7 信息对话框的按钮屏蔽键盘操作,只允许鼠标点击
  11. 用注解的方式实现Mybatis插入数据时返回自增的主键Id
  12. SimpleDateFormat安全的时间格式化
  13. 从RDS中获取binlog
  14. 漫谈可视化Prefuse(六)
  15. apache分割数组和集合的分法
  16. csrf漏洞
  17. 机器学习之路:python 多项式特征生成PolynomialFeatures 欠拟合与过拟合
  18. eclipse中java项目转成Web项目
  19. Delphi 正则表达式之TPerlRegEx 类的属性与方法(4): Replace
  20. windows 共存多个位数不同的jdk时,eclipse的报错对应措施

热门文章

  1. Oracle性能优化之内存管理
  2. 32bit和64bit系统的区别,运行机制浅析
  3. HDLBits答案——Verification: Writing Testbenches
  4. mysql 在连接表中的要点
  5. C# Math 中的常用的数学运算
  6. vulnhub靶场之CONTAINME: 1
  7. 【Java SE】Day09 继承、super、this、抽象类
  8. python读入中文文本编码错误
  9. Linux第四章 进程
  10. IPv4和IPv6地址的存取