如何使用python从excel读取并写入json文件? [英] How to read from excel and write in json file using python?
本文介绍了如何使用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
这篇关于如何使用python从excel读取并写入json文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文