废话不说,直接上代码:
import xlrd
import xlwt
# 读excel然后写到mysql的套路
def updata_info():
book = xlrd.open_workbook("tb_yuzhao.xls")
sheet = book.sheet_by_name("sheet1") # 建立数据库连接
db = MySQLdb.connect(。。。。。。。)
# 游标对象,用于数据库逐行遍历
cursor = db.cursor() # 创建一个for循环迭代读取xls文件每行数据的, 从第二行开始是要跳过标题
for r in range(1, sheet.nrows):
code_name = sheet.cell(r, 0).value
multi_desc = sheet.cell(r, 3).value
# 执行sql语句
update_sql = 'update yuzhao set desc = "{0}" where code_name = "{1}"'.format(desc, code_name)
print update_sql
cursor.execute(update_sql)
cursor.close()
db.commit()
db.close()
print 'Done!' # 写excel的套路
def write_excel(filename, data):
book = xlwt.Workbook() # 创建excel对象
sheet = book.add_sheet('sheet1') # 添加一个表
c = 0 # 保存当前列
for d in data: # 取出data中的每一个元组存到表格的每一行
for index in range(len(d)): # 将每一个元组中的每一个单元存到每一列
sheet.write(c, index, d[index])
c += 1
book.save(filename) # 保存excel

最新文章

  1. es6小白学习笔记(一)
  2. [转]C/C++ 程序员必须收藏的资源大全
  3. maven web项目build失败
  4. hdu 1425 sort 解题报告
  5. jquery的监听事件和触发事件
  6. codeforces 682C Alyona and the Tree DFS
  7. 总结XX网app中webapp常见的前端错误。
  8. python----特性002
  9. 23. leetcode 169. Majority Element
  10. Project 2:传奇汉诺塔
  11. linux正确的关机方法
  12. centos7搭建zabbix3.0监控系统
  13. 正则求解@" (?<=^\[length=)(\d+)(?=\])"
  14. [ZJOI2006]超级麻将(可行性dp)
  15. Mac 建PHP 环境 及 配置 apache 默认目录
  16. C# SQLite数据库
  17. 1458 Common Subsequence
  18. ThinkPHP 3.2 用户注册邮箱验证激活帐号
  19. javascript随笔和常见的知识点
  20. C++ leetcode::two sum

热门文章

  1. HTML5存储(带一个粗糙的打怪小游戏案例)
  2. 使用mongoose--写接口
  3. 怎么解决MySQL密码问题
  4. python 队列、栈
  5. gulp常用插件之del使用
  6. koa文档笔记
  7. linux--权限管理和用户管理
  8. 主机名由localhost变成bogon是怎么回事,怎样变回localhost这个名字?
  9. 被遗忘的宝藏-LaTeX发行版自带字体&自己一些字体心得
  10. 微信小程序期末复习