读Excel并写入json文件

#!/usr/bin/python

# -*- coding: utf-8

import xlrd

import json

def read_excel():

    table = xlrd.open_workbook("./querynote.xlsx")

    sheet = table.sheet_by_name("querynote")

    b = {}

    for rowNum in range(sheet.nrows):

        for colNum in range(sheet.ncols):

            cell = sheet.cell(rowNum, colNum)

                if colNum ==1:

                    discription = cell.value

                elif colNum ==2:

                    value = cell.value

       b["{}".format(discription)] = value

#把字典写入json文件

    file ="a.json"

    with open(file,"w")as f:

        d = json.dumps(b,ensure_ascii=False)

        f.write(d)

#打印第一列的所有值

    for rowNumin range(sheet.nrows):

            colNum =1

            cell = sheet.cell(rowNum,colNum)

            value = cell.value

            print(value)

if __name__ =="__main__":

m = read_excel()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。