欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3 循環(huán)讀取excel文件并寫入json操作

 更新時間:2020年07月14日 09:18:40   作者:qubeijun  
這篇文章主要介紹了python3 循環(huán)讀取excel文件并寫入json操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

文件內(nèi)容:

excel內(nèi)容:

代碼:

import xlrd
import json
import operator
 
def read_xlsx(filename):
 # 打開excel文件
 data1 = xlrd.open_workbook(filename)
 # 讀取第一個工作表
 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ù)形成一個列表
  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)打開每個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))

結(jié)果:

補(bǔ)充知識:Python mysql數(shù)據(jù) 讀取時間參數(shù) for循環(huán)寫入Excel文件

最近在利用Python 實(shí)現(xiàn)自動化表報(bào)時,有個功能是mysql的業(yè)務(wù)時間是讀取模板文件的時間參數(shù),需要用到for循環(huán)功能,基本思路是:

1.自動創(chuàng)建一個輸出文件的文件夾

2.根據(jù)模板文件創(chuàng)建一個新的excel文件到新創(chuàng)建的文件夾中

3.每次寫入時返回sheet的最大行數(shù)max_row,下次寫入時從最大行的下一行開始繼續(xù)寫入

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')
 
# 打開數(shù)據(jù)庫連接
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)建文件時如果路徑不存在會創(chuàng)建這個路徑
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['時間配置']
for t in range(2, timet.max_row + 1): # 讀取配置表中的時間
  savedata(timet.cell(t, 1).value, timet.cell(t, 2).value)
 

5.模板文件的時間參數(shù)設(shè)置如下:

以上這篇python3 循環(huán)讀取excel文件并寫入json操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python如何提取公共模塊并避免代碼重復(fù)

    Python如何提取公共模塊并避免代碼重復(fù)

    模塊化編程是提高代碼重用性和可維護(hù)性的關(guān)鍵,這篇文章小編就來為大家詳細(xì)介紹一下Python如何提取公共模塊并避免代碼重復(fù),希望對大家有所幫助
    2025-02-02
  • Python實(shí)現(xiàn)購物車購物小程序

    Python實(shí)現(xiàn)購物車購物小程序

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)購物車購物小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 用Python編寫生成樹狀結(jié)構(gòu)的文件目錄的腳本的教程

    用Python編寫生成樹狀結(jié)構(gòu)的文件目錄的腳本的教程

    這篇文章主要介紹了用Python編寫生成樹狀結(jié)構(gòu)的文件目錄的腳本的教程,是一個利用os模塊下各函數(shù)的簡單實(shí)現(xiàn),需要的朋友可以參考下
    2015-05-05
  • python設(shè)置中文界面實(shí)例方法

    python設(shè)置中文界面實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于python設(shè)置中文界面實(shí)例方法,有興趣的朋友們可以學(xué)習(xí)參考下。
    2020-10-10
  • PyQt5實(shí)現(xiàn)簡單的計(jì)算器

    PyQt5實(shí)現(xiàn)簡單的計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了PyQt5實(shí)現(xiàn)簡單的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • python隨機(jī)3分鐘發(fā)送一次消息完整代碼

    python隨機(jī)3分鐘發(fā)送一次消息完整代碼

    最近我接到這樣的任務(wù)需求有一個實(shí)時任務(wù),想要間隔3分鐘發(fā)送,最近的一次消息,接下來通過本文給大家分享python隨機(jī)3分鐘發(fā)送一次消息,需要的朋友可以參考下
    2024-03-03
  • Python中的Pydantic序列化詳解

    Python中的Pydantic序列化詳解

    這篇文章主要介紹了Python中的Pydantic序列化詳解,Pydantic 是 Python 中一個高性能的數(shù)據(jù)驗(yàn)證和序列化庫,它提供了一個簡單而強(qiáng)大的方式來定義結(jié)構(gòu)化的數(shù)據(jù),并在應(yīng)用程序的各個層次中使用這些數(shù)據(jù),需要的朋友可以參考下
    2023-10-10
  • 教你掌握分布式訓(xùn)練PyTorch?DDP到Accelerate到Trainer

    教你掌握分布式訓(xùn)練PyTorch?DDP到Accelerate到Trainer

    這篇文章主要為大家介紹了教你掌握分布式訓(xùn)練PyTorch?DDP到Accelerate到Trainer
    2023-02-02
  • 一文秒懂python中的 \r 與 end=‘’ 巧妙用法

    一文秒懂python中的 \r 與 end=‘’ 巧妙用法

    這篇文章主要介紹了python中的 \r 與 end=‘’ 巧妙用法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • python中退出多層循環(huán)的方法

    python中退出多層循環(huán)的方法

    這篇文章主要介紹了python中退出多層循環(huán)的方法,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11

最新評論