如何使用python从excel读取并写入json文件?

问题描述:

我正在尝试创建一个从 test.xlsx 读取数据的 json 文件.我的示例代码如下.

I am trying to create a json file which reads data from test.xlsx. My sample code is below.

我希望从 Excel 工作表中读取它而不是WO-12345"和其他值,就像我希望从 Excel 中的特定单元格中读取它一样.

Instead of "WO-12345" and other values, I want that to be read from the excel sheet, like I want it to be read from a particular cell in excel.

import xlrd
from collections import OrderedDict
import simplejson as json
import json

jsonfile = open('data1.json', 'w')
data_list = []
data = OrderedDict()

data['workOrder'] = "WO-12345"
data['alternateStart'] = "2018-01-13T10:00:00Z"
data['mobileNumber'] = "(555) 555-5555"
data['officeNumber'] = "(555) 555-5554"
data['description'] = "Testing"
data['equipment'] = "Testing"

data_list.append(data)

j = json.dumps(data_list)


json.dump(data, jsonfile, indent=3, sort_keys=False)
jsonfile.write('\n')

如果你想读取 Excel 中的 Pandas pandas.read_excel,它会返回一个 pandas.DataFrame具有 to_json 方法.

If you want to read an Excel there's pandas pandas.read_excel, it returns a pandas.DataFrame that has the to_json method.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html