python3 循環(huán)讀取excel文件并寫(xiě)入json操作
文件內(nèi)容:

excel內(nèi)容:

代碼:
import xlrd
import json
import operator
def read_xlsx(filename):
# 打開(kāi)excel文件
data1 = xlrd.open_workbook(filename)
# 讀取第一個(gè)工作表
table = data1.sheets()[0]
# 統(tǒng)計(jì)行數(shù)
n_rows = table.nrows
data = []
# 微信文章屬性:wechat_name wechat_id title abstract url time read like number
for v in range(1, n_rows-1):
# 每一行數(shù)據(jù)形成一個(gè)列表
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],
})
# 返回所有數(shù)據(jù)
return data
if __name__ == '__main__':
d = []
# 循環(huán)打開(kāi)每個(gè)excel
for i in range(1, 16):
d1 = read_xlsx('./excel data/'+str(i)+'.xlsx')
d.extend(d1)
# 微信文章屬性
# 按時(shí)間升序排列
d = sorted(d, key=operator.itemgetter('time'))
# 寫(xiě)入json文件
with open('article.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(d, ensure_ascii=False, indent=2))
name = []
# 微信id寫(xiě)文件
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))
結(jié)果:

補(bǔ)充知識(shí):Python mysql數(shù)據(jù) 讀取時(shí)間參數(shù) for循環(huán)寫(xiě)入Excel文件
最近在利用Python 實(shí)現(xiàn)自動(dòng)化表報(bào)時(shí),有個(gè)功能是mysql的業(yè)務(wù)時(shí)間是讀取模板文件的時(shí)間參數(shù),需要用到for循環(huán)功能,基本思路是:
1.自動(dòng)創(chuàng)建一個(gè)輸出文件的文件夾
2.根據(jù)模板文件創(chuàng)建一個(gè)新的excel文件到新創(chuàng)建的文件夾中
3.每次寫(xiě)入時(shí)返回sheet的最大行數(shù)max_row,下次寫(xiě)入時(shí)從最大行的下一行開(kāi)始繼續(xù)寫(xiě)入
4.每次讀取必須為同一個(gè)文件
代碼如下:
#! /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')
# 打開(kāi)數(shù)據(jù)庫(kù)連接
db = MySQLdb.connect(host="localhost", user="zimu", passwd="zimu", db="xxx", port=0000,charset='utf8')
template_file_demo = r"D:\path\demo.xlsx"
# makedirs 創(chuàng)建文件時(shí)如果路徑不存在會(huì)創(chuàng)建這個(gè)路徑
output_path = r"D:\output\demo"+"_"+ time.strftime("%Y%m%d", time.localtime()) +"_" + str(int(time.time()))+"\\"
os.makedirs(output_path)
#創(chuàng)建文件到新創(chuàng)建的文件夾中
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['時(shí)間配置']
for t in range(2, timet.max_row + 1): # 讀取配置表中的時(shí)間
savedata(timet.cell(t, 1).value, timet.cell(t, 2).value)
5.模板文件的時(shí)間參數(shù)設(shè)置如下:

以上這篇python3 循環(huán)讀取excel文件并寫(xiě)入json操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 列表 sort()函數(shù)使用實(shí)例詳解
這篇文章主要介紹了Python 列表 sort()函數(shù)使用詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
pandas刪除重復(fù)數(shù)據(jù)簡(jiǎn)單方法
這篇文章主要給大家介紹了關(guān)于pandas刪除重復(fù)數(shù)據(jù)的簡(jiǎn)單方法,在數(shù)據(jù)處理過(guò)程中常常會(huì)遇到重復(fù)的問(wèn)題,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
Python 實(shí)現(xiàn)隨機(jī)數(shù)詳解及實(shí)例代碼
這篇文章主要介紹了Python 實(shí)現(xiàn)隨機(jī)數(shù)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
django1.11.1 models 數(shù)據(jù)庫(kù)同步方法
今天小編就為大家分享一篇django1.11.1 models 數(shù)據(jù)庫(kù)同步方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
pip安裝庫(kù)報(bào)錯(cuò)[notice]?A?new?release?of?pip?available:?22.2
這篇文章主要給大家介紹了關(guān)于pip安裝庫(kù)報(bào)錯(cuò)[notice]?A?new?release?of?pip?available:?22.2?->?22.2.2的相關(guān)資料,文中通過(guò)圖文將解決的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Python一句代碼實(shí)現(xiàn)找出所有水仙花數(shù)的方法
今天小編就為大家分享一篇Python一句代碼實(shí)現(xiàn)找出所有水仙花數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Python基礎(chǔ)教程之控制結(jié)構(gòu)詳解
Python中有三大控制結(jié)構(gòu),分別是順序結(jié)構(gòu)、分支結(jié)構(gòu)(選擇結(jié)構(gòu))以及循環(huán)結(jié)構(gòu),任何一個(gè)項(xiàng)目或者算法都可以使用這三種結(jié)構(gòu)來(lái)設(shè)計(jì)完成,這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)教程之控制結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2021-11-11
開(kāi)源軟件包和環(huán)境管理系統(tǒng)Anaconda的安裝使用
Anaconda是一個(gè)用于科學(xué)計(jì)算的Python發(fā)行版,支持 Linux, Mac, Windows系統(tǒng),提供了包管理與環(huán)境管理的功能,可以很方便地解決多版本python并存、切換以及各種第三方包安裝問(wèn)題。2017-09-09

