美文网首页
python处理文字,读取execl表格入库

python处理文字,读取execl表格入库

作者: 阿尼奥赛哟 | 来源:发表于2020-08-19 14:10 被阅读0次

python读取嵌套字典 eval str---->dict

列表去掉{} https://www.php.cn/python-tutorials-423935.html

什么时候使用JSON对象(字典)什么时候使用JSON文本? 一般在组装data参数时,建议使用字典格式,发送请求时用json.dumps(data)转化为文本发送,收到请求

后使用json.loads(res.text)转化为字典,方便我们获取其中的参数信息

import pymysql

from docx import Document

db=pymysql.connect(host="172.16.22.208",port=3316,user="root",password="123456789Aa",database="hydrological_dev",charset="utf8")

cursor=db.cursor()

path= "C:\\Users\\Administrator\\Desktop\\2017年城市防汛积水隐患调查表3.docx"          #文件路径

document= Document(path)                #读入文件

tables= document.tables                    #获取文件中的表格集

table= tables[0]                                  #获取文件中的第一个表格

t=181

for i in range(49,len(table.rows)):                              #从表格第二行开始循环读取表格数据

   index1=int(table.cell(i,0).text)

   stnm=table.cell(i,1).text

   r= table.cell(i,8).text.split(',\n')

  x= r[0]

  y= r[1]

sql="insert into test(index1,stnm,lgtd,lttd,expand1,expand2,index2) values ('%s','%s','%s','%s',3,'2017年城市防汛积水隐患调查表',%d)"%(index1,stnm,x,y,t)

try:

    cursor.execute(sql)

    db.commit()

    print("successfully")

except:

    db.rollback()

    db.close()

    print("fail")

    print(sql)

t=t+1

相关文章

网友评论

      本文标题:python处理文字,读取execl表格入库

      本文链接:https://www.haomeiwen.com/subject/wawrjktx.html