#-*- coding:utf-8 -*-
# 处理 excel 中的 area 为 Mysql insert 语句
import xlrd, json, codecs, os # data = xlrd.open_workbook('101.xls')
data = xlrd.open_workbook('C:\Users\Administrator\Desktop\changanyiyuan.xlsx')
# table = data.sheets()[0]
table = data.sheet_by_index(0) # try
hospitalJson = codecs.open('C:\Users\Administrator\Desktop\changanyiyuan.json','a+','utf-8')
hospitalJson.truncate(0)
hospitalJson.write('[') # 先写入数组头 hospitalJsonInsert = codecs.open('C:\Users\Administrator\Desktop\hospitalJsonInsert.json','a+','utf-8')
hospitalJsonInsert.truncate(0)
hospitalJsonInsert.write('insert into department(hospital_id, pid, name, rank, create_time, modify_time) values') # 取出表格的第一行作为 json 的 key (javascript object notation)
attribute = table.row_values(0) # 除第一行以外的数据放入一个 dict
jsonAll = [] contents = {}
for index_r in range(1,table.nrows):
for index_c in range(table.ncols):
contents[attribute[index_c]] = table.cell(index_r, index_c).value # cell 单元格的一个属性并非方法
# print contents
hospitalJson.write("%s,\n" %json.dumps(contents,ensure_ascii=False,sort_keys=True, indent=4))
hospitalJsonInsert.write('(2265, -1, \'%s\' ,%d, now(), now()),'%(table.cell(index_r, 3).value, table.cell(index_r, 5).value))
# jsonAll.append(json.dumps(contents,ensure_ascii=False,sort_keys=True, indent=4)) # print hospitalJson.tell()
# hospitalJson.seek(0)
# print hospitalJson.tell() hospitalJson.seek(-2, os.SEEK_END)
hospitalJson.truncate()
hospitalJson.write(']') # 数组结束 # hospitalJson.write(json.dumps(contents,ensure_ascii=False,sort_keys=True, indent=4)) # 少了 ensure_ascii=False 参数就不能写中文进文件?
hospitalJsonInsert.seek(-1, os.SEEK_END)
hospitalJsonInsert.truncate()
hospitalJsonInsert.write(';') # hospitalJson.write("%s" % jsonAll)
hospitalJson.close()
hospitalJsonInsert.close() # (`id`, `hospital_id`, `pid`, `name`, `rank`, `create_time`, `modify_time`)
# 写成 mysql 语句
# hospitalJsonInsert.write('( 2265, -1, \'%s\' ,%s, now(), now())'%(table.cell(index_r, 3), table.cell(index_r, 5)))

使用 xlrd 模块实现对excel 的读取、excel转json 、excel 转 mysql insert 语句的更多相关文章

  1. 【POI word】使用POI实现对Word的读取以及生成

    项目结构如下: 那第一部分:先是读取Word文档 package com.it.WordTest; import java.io.FileInputStream; import java.io.Fil ...

  2. 利用http协议使用普通的网站虚拟主机+安信可A6C GPRS模块实现对stm32的远程升级

    步骤: 1.生成bin文件并将bin文件放到虚拟主机目录内 2.做一个php的页面分块读取该bin文件,以asc字符echo出来,并标记好头尾长度和校验. 3.GPRS协议栈连接主机路径,收到内容,将 ...

  3. excel 里面拼接 MySQL insert 语句

    ="('"&A2&"',"&" '"&B2&"','"&C2&& ...

  4. python 利用三方的xlrd模块读取excel文件,处理合并单元格

      目的: python能使用xlrd模块实现对Excel数据的读取,且按照想要的输出形式.  总体思路: (1)要想实现对Excel数据的读取,需要用到第三方应用,直接应用. (2)实际操作时候和我 ...

  5. 通过脚本实现对web的健康检查

    前面的文章中(https://www.cnblogs.com/zyxnhr/p/10707932.html),通过nginx的第三方模块实现对web端的一个监控,现在通过一个脚本实现对第三方的监控 脚 ...

  6. python读写Excel文件--使用xlrd模块读取,xlwt模块写入

    一.安装xlrd模块和xlwt模块 1. 下载xlrd模块和xlwt模块 到python官网http://pypi.python.org/pypi/xlrd下载模块.下载的文件例如:xlrd-0.9. ...

  7. $ 用python处理Excel文档(1)——用xlrd模块读取xls/xlsx文档

    本文主要介绍xlrd模块读取Excel文档的基本用法,并以一个GDP数据的文档为例来进行操作. 1. 准备工作: 1. 安装xlrd:pip install xlrd 2. 准备数据集:从网上找到的1 ...

  8. 利用xlrd模块实现Python读取Excel文档

    # -*- coding: cp936 -*- #python读取excel import xlrd def main(): xls=xlrd.open_workbook("d:\\11.x ...

  9. Python xlrd模块读取Excel表中的数据

    1.xlrd库的安装 直接使用pip工具进行安装(当然也可以使用pycharmIDE进行安装,这里就不详述了) pip install xlrd 2.xlrd模块的一些常用命令 ①打开excel文件并 ...

随机推荐

  1. WPF外包公司——北京动点飞扬软件:开发企业WPF项目需要掌握些什么

    做为企业开发一个WPF项目,对于很多不熟悉微软WPF技术和XAML语言开发团队而言,北京动点飞扬在此给各位一点建议: 1.首先开发团队要整体对于XAML和WPF的运作机制熟悉. 2.开发人员起码要会用 ...

  2. Internetware网构软件(摘抄)

    The Internet provides a global open infrastructure for exchanging and sharing of various resources f ...

  3. 在VBA中调用winsock控件

    如果系统没有Winsock控件的话,可以下载下面的控件MSWINSCK.OCX,然后将该文件复制到C:\Windows\System32目录下. 在VBE窗口中,从菜单"工具"-& ...

  4. Linux定时任务系统 Cron

    运行计划任务时:service crond restart提示:crond: unrecognized service安装计划任务:yum -y install vixie-cron 另外附计划任务的 ...

  5. HttpWebRequest.GetResponse 方法 转载

    GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象. 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类 ...

  6. powerDesigner 报Unable to connect SQLState=08004 解决方法

    在使用PowerDesigner配置数据库连接(configure connections)的时候,点击Test connection之后弹出Unable to connect SQLState=08 ...

  7. [DFNews] GetData也出取证软件了

    从事计算机取证的应该都听说过MIP(Mount Image Pro).VFC仿真和Recover My Files,上述三个应用比较广泛的软件都是GetData公司的产品.GetData现在也推出了自 ...

  8. AX Dynamic 2012 SSRS 按行数分页

    按行数分页 1. Create a new Row Group with the following grouping expression: =Ceiling(RowNumber(Nothing)/ ...

  9. CultureInfo 类

    https://msdn.microsoft.com/zh-cn/library/system.globalization.cultureinfo(VS.80).aspx 区域性名称 区域性标识符 语 ...

  10. 九、DAG hierarchy

    DAG 节点有两种,Transformation/shape. shape节点是transformation的子节点. transformation节点包括position, rotation, sc ...