python将数据写入excel代码,python与office交互
# -*- coding: utf-8 -*-
from smartframe.header import *
import pymysql
import json
import importlib,sys
importlib.reload(sys)
import re
import xlwt
from xlwt import Workbook, Formula
import xlrd ##连接数据库
conn =pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='root',db='XXXX',charset='utf8')
# 创建游标# # 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 执行sql,并返回受到的影响行数##### #sql='select * from ceshi'
cursor.execute(sql)
# 获取所有数据########
result = cursor.fetchall()
#获取获取第一条数据#####
#result = cursor.fetchone()
#print(result)
#print (len(result))
datanew = {}
for item in result:
#print(item)
datanew[item['user_id']] = list(eval(item['answer']))
#print(datanew)
book = Workbook()
sheet1 = book.add_sheet('Sheet 1') #字典数据
#循环字典key 的值
num = [a for a in datanew]
#num1=[]
#for a in datax:
# print (a)
# if a not in num1:
# num1.append(a)
#num.sort()
#print(num)
lennum=len(num)
#print(lennum)
for a in range(lennum): #print(num[a])
lena=len(datanew[num[a]]); #print(lena)
lena1=datanew[num[a]];
lena1.sort()
#print(lena1)
a1=num[a];
sheet1.write(a+1,0,a1)
#print(lenanew) b1x=[]
for b in range(lena):
#print(b)
#print (lena1[b]);
#print((lena1[b])[0])
#print((lena1[b])[1])
b1=(lena1[b])[0]
b2=(lena1[b])[1]
b1x.append(b1);
#print(b)
sheet1.write(a+1,b+1,b2)
#print(b2)
if a==0:
#print(b1x)
for y in range (len(b1x)):
bx=b1x[y]
# print(b1x[y])
#print(y)
sheet1.write(0,y+1,bx) book.save('C:/Users/Administrator/Desktop/ceshi0_1.xls') conn.commit()
#获取自增id
new_id = cursor.lastrowid
print (new_id) # 关闭游标
cursor.close()
# 关闭连接
conn.close()
python将数据写入excel代码,python与office交互的更多相关文章
- 使用python将数据写入excel
先来个简单的例子: #!/usr/bin/python #coding=utf-8 # ======================================================== ...
- python读取数据写入excel
'''写入excel文件''' import xlsxwriter # todo 创建excel文件 xl = xlsxwriter.Workbook(r'D:\testfile\test.xlsx' ...
- Python将数据写入excel或者txt,读入csv格式或xls文件,写入csv(写一行空一行解决办法)
1.写入excel,一开始不需要自己新建一个excel,会自动生成 attribute_proba是我写入的对象 import xlwt myexcel = xlwt.Workbook() sheet ...
- python 将数据写入excel
摘要链接: python第三方库——xlrd和xlwt操作Excel文件学习 :http://blog.csdn.net/wangkai_123456/article/details/50457284 ...
- python读取数据写入excel的四种操作
Python对Excel的读写主要有:xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种 xlutils结合xlrd: 操作的是以xls后缀的excel,读取文件保留原格式 ...
- Python:将爬取的网页数据写入Excel文件中
Python:将爬取的网页数据写入Excel文件中 通过网络爬虫爬取信息后,我们一般是将内容存入txt文件或者数据库中,也可以写入Excel文件中,这里介绍关于使用Excel文件保存爬取到的网页数据的 ...
- 初识python: xlsxwriter 将数据写入Excel
使用 xlsxwriter 模块将数据写入excel . #!/user/bin env python # author:Simple-Sir # time:2020/9/24 12:51 # 使用 ...
- Python小数据池,代码块
今日内容一些小的干货 一. id is == 二. 代码块 三. 小数据池 四. 总结 python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. ...
- python 小数据池,代码块, is == 深入剖析
python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了, ...
随机推荐
- 基于ASP.MVC票据FormsAuthenticationTicket身份认证
做一个最基础的业务需求用户登录,将此用户的身份发回到客户端的Cookie,之后此用户再访问这个web应用就会连同这个身份Cookie一起发送到服务端.服务端上的授权设置就可以根据不同目录对不同用户的访 ...
- Intellij IDEA 4种配置热部署的方法
热部署可以使的修改代码后,无须重启服务器,就可以加载更改的代码. 第1种:修改服务器配置,使得IDEA窗口失去焦点时,更新类和资源 菜单Run -> EditConfiguration , 然后 ...
- SpringCloud四:hystrix-propagation
注:pom.xml 及配置文件配置与上篇相同 package com.itmuch.cloud.controller; import org.springframework.beans.factory ...
- HTML基础教程-段落
HTML 段落 段落是通过 <p> 标签定义的. <p>This is a paragraph</p> <p>This is another parag ...
- Python Xcode搭建Python环境以及使用PyCharm CE
pycharm CE下载 使用教程 1.基础学习网站 2.在Xcode7中搭建python开发环境,这个不行了就试试第二个,我是第二个可以正常输出了,第一个没有输出 3.Python学习-MAC下 ...
- 监听键盘弹起View上调
可以用三方库IQKeyboardManager 用这个第三方 http://www.jianshu.com/p/f8157895 #pragma mark - keyboard events - // ...
- button的用法
C# 如何去掉button按钮的边框线? 设置FlatStyle为Flat,并且设置FlatAppearance下的BorderSize为0.
- Hybris 项目工程配置
1.控制台页面进入platform目录 cd F:\hybris640\hybris\bin\platform 并运行 setantenv.bat 生成对应的ant. 2.运行 ant moduleg ...
- python访问http的GET/POST
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7562295.html 作者:窗户 Q ...
- JDK8.0新特性
连接转载地址:http://www.2cto.com/kf/201609/544044.html Eclipse: http://aiyiupload.oss-cn-beijing.aliyuncs. ...