我正在创建一个 ML 模型,它将使用 JSON 文件来理解模式和响应格式。由于我的数据为 excel 格式,因此我在 python 中将其转换为 JSON。
这是代码:
import xlrd
from collections import OrderedDict
import simplejson as json
# Open the workbook and select the first worksheet
wb = xlrd.open_workbook('D:\\android\\testdata2.xlsx')
sh = wb.sheet_by_index(0)
# List to hold dictionaries
data_list = []
# Iterate through each row in worksheet and fetch values into dict
for rownum in range(1, sh.nrows):
data = OrderedDict()
row_values = sh.row_values(rownum)
data['pattern'] = row_values[0]
data['response'] = row_values[1]
data_list.append(data)
# Serialize the list of dicts …Run Code Online (Sandbox Code Playgroud)