日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3读写excel文件_python3 循环读取excel文件并写入json操作

發布時間:2025/3/20 python 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3读写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操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。

時間: 2020-07-14

總結

以上是生活随笔為你收集整理的python3读写excel文件_python3 循环读取excel文件并写入json操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。