python读excel并写入_python3 循环读取excel文件并写入json操作
文件内容:
excel内容:
代码:
import xlrd
import json
import operator
def read_xlsx(filename):
# 打开excel文件
data1 = xlrd.open_workbook(filename)
# 读取第一个工作表
table = data1.sheets()[0]
# 统计行数
n_rows = table.nrows
data = []
# 微信文章属性:wechat_name wechat_id title abstract url time read like number
for v in range(1, n_rows-1):
# 每一行数据形成一个列表
values = table.row_values(v)
# 列表形成字典
data.append({'wechat_name': values[0],
'wechat_id': values[1],
'title': values[2],
'abstract': values[3],
'url': values[4],
'time': values[5],
'read': values[6],
'like': values[7],
'number': values[8],
})
# 返回所有数据
return data
if __name__ == '__main__':
d = []
# 循环打开每个excel
for i in range(1, 16):
d1 = read_xlsx('./excel data/'+str(i)+'.xlsx')
d.extend(d1)
# 微信文章属性
# 按时间升序排列
d = sorted(d, key=operator.itemgetter('time'))
# 写入json文件
with open('article.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(d, ensure_ascii=False, indent=2))
name = []
# 微信id写文件
f1 = open('wechat_id.txt', 'w')
for i in d:
if i['wechat_id'] not in name:
name.append(i['wechat_id'])
f1.writelines(i['wechat_id'])
f1.writelines('\n')
print(len(name))
结果:
补充知识:Python mysql数据 读取时间参数 for循环写入Excel文件
最近在利用Python 实现自动化表报时,有个功能是mysql的业务时间是读取模板文件的时间参数,需要用到for循环功能,基本思路是:
1.自动创建一个输出文件的文件夹
2.根据模板文件创建一个新的excel文件到新创建的文件夹中
3.每次写入时返回sheet的最大行数max_row,下次写入时从最大行的下一行开始继续写入
4.每次读取必须为同一个文件
代码如下:
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import MySQLdb
from openpyxl import load_workbook
import sys
import time
import os
reload(sys)
sys.setdefaultencoding('utf8')
# 打开数据库连接
db = MySQLdb.connect(host="localhost", user="zimu", passwd="zimu", db="xxx", port=0000,charset='utf8')
template_file_demo = r"D:\path\demo.xlsx"
# makedirs 创建文件时如果路径不存在会创建这个路径
output_path = r"D:\output\demo"+"_"+ time.strftime("%Y%m%d", time.localtime()) +"_" + str(int(time.time()))+"\\"
os.makedirs(output_path)
#创建文件到新创建的文件夹中
book_demo = load_workbook(template_file_demo)
book_demo.save(output_path + "demo" +"_"+time.strftime("%Y%m%d", time.localtime())+".xlsx")
#读取指定文件夹下的文件
demo_file = output_path+"demo"+"_"+time.strftime("%Y%m%d", time.localtime())+".xlsx"
def savedata(start_time,end_time):
demosql = '''select * from demo where start_date<='%s' and end_date>='%s''''%(start_time,end_time)
cursor = db.cursor()
cursor.execute(demosql)
demodata = cursor.fetchall()
demo_book = load_workbook(demo_file)
demosheet = demo_book['demo']
row_t = demosheet.max_row
i = 0
while i < len(demodata):
for j in range(0, 8):
demosheet.cell(row_t + i + 1, j + 1).value = demodata[i][j]
i += 1
demo_book.save(output_path+"demo"+"_"+time.strftime("%Y%m%d", time.localtime())+".xlsx")
book_template = load_workbook(template_file_demo)
timet = book_template['时间配置']
for t in range(2, timet.max_row + 1): # 读取配置表中的时间
savedata(timet.cell(t, 1).value, timet.cell(t, 2).value)
5.模板文件的时间参数设置如下:
以上这篇python3 循环读取excel文件并写入json操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。
python读excel并写入_python3 循环读取excel文件并写入json操作相关推荐
- python循环读取excel存入列表_python3 循环读取excel文件并写入json操作
文件内容: excel内容: 代码: import xlrd import json import operator def read_xlsx(filename): # 打开excel文件 data ...
- java代码里的JSON格式怎么写好看_python3 循环读取excel文件并写入json操作
文件内容: excel内容: 代码: import xlrdimport jsonimport operatordef read_xlsx(filename): # 打开excel文件 data1 = ...
- python3读写excel文件_python3 循环读取excel文件并写入json操作
文件内容: excel内容: 代码: import xlrd import json import operator def read_xlsx(filename): # 打开excel文件 data ...
- python写入指定路径的文件_python 从shell读取指定文件以及写入指定文件
python 从shell读取指定文件以及写入指定文件 运行不同的文件每次都要修改脚本,能不能指定输入文件以及输出文件? python abstract_element.py 1.txt 2.txt ...
- robot framework循环读取excel表操作
robot framework循环读取excel表操作 1.安装excellibrary,注意python3.7版本,需安装:robotframework-excel,robotframework-e ...
- 【Python入门教程】第72篇 读取CSV文件
本篇我们介绍如何使用 Python 内置的 csv 模块读取 CSV 文件. CSV 文件 CSV 代表逗号分隔值(comma-separated values),CSV 文件就是使用逗号分隔数据的文 ...
- php读取excel并导入数据,PHPExcel读取excel并导入数据库
PHPExcel是一款php对于excel数据表读写的一个非常棒的插件了,下面我来给大家介绍利用PHPExcel读取excel并导入mysql数据库方法. 例1,代码示例,代码如下:require_o ...
- java读取txt文件函数_Java读取txt文件和写入txt文件的简单实例
写Java程序时经常碰到要读如txt或写入txt文件的情况,但是由于要定义好多变量,经常记不住,每次都要查,特此整理一下,简单易用,方便好懂! package edu.thu.keyword.test ...
- Flutter入门系列-Flutter读取assets文件并写入应用程序路径
一.思考 在Android开发中经常会遇到需要将asset中的文件拷贝到本地目录中,所以Flutter 是否有能够读取 asset 中文件的API呢?rootBundle 就可以解决该问题. 二.代码 ...
最新文章
- AS3 Socket 基础知识(很全面)
- Windows下SVN权限配置过程详解
- pytorch统计矩阵非0的个数_PyTorch常见的12坑
- linux终端出现bash: setup.bash: No such file or directory,和.bashrc文件的问题
- Uber 开源 Piranha,可自动删除过时代码
- JavaScript中的nodeName nodeType nodeValue区别
- 一个按键控制数码管的开和关_三菱PLC数码管显示及按键控制实验
- linux非阻塞的socket发送数据出现EAGAIN错误的处理方法
- 一文讲清K8s如何改变美团的云基础设施
- 深入理解CPU cache:组织、一致性(同步)、编程
- 小程序不同页面之间通讯的解决方案 1
- Hadoop大数据简介
- Silverlight 4 or 5 安装步骤
- rar x64 5.50 linux,WinRAR 5.50简体中文注册版(已含Key文件和32位、64位)
- win10 + ubuntu20.04 双系统卸载(删除) ubuntu20.04 (图文)
- matlab图像去散焦,matlab为图像加运动模糊和散焦模糊
- php定义一个矩形类rectangle,Python3面向对象—点和矩形类
- win10系统日语输入法只能打出英文字母无法切换微软输入法无法使用
- FLOJET GP50/7 PT496976
- CSS 样式书写顺序及规范
热门文章
- 一本通-提高篇-图论-割点和桥:
- ORACLE 11G中的ADR介绍:
- codeforces1304F1 dp
- 我们来用Unity做个2D像素boss战
- 用计算机创造一个宇宙,宇宙是一个巨大的计算机,外星文明创造出人类,我们不是真实的?...
- 从12大技巧、30个案例、99个模板谈怎么写标题
- 【华为OD机试真题 python】优秀学员统计【2022 Q4 | 100分】
- python简单实现注意力机制
- 暴利:网友惊爆域名注册底价
- Mysql5.7, 千万数据快速插入解决方案( JDBC方式, 百秒搞定!!!)