【发布时间】:2019-05-12 14:49:09
【问题描述】:
我正在尝试从 XLS 文件数据更新 json 文件。 这是我想做的:
- 从Json 中提取名称
- 从 XLS 中提取名称
- 对于 namesFromXLS 中的 nameFromXLS:
- 检查 nameFromXLS 是否在 namesFromJson 中:
- 如果为真:那么:
- 提取 xls 行(同名)
- 更新 jsonFile(同名)
- 如果为真:那么:
- 检查 nameFromXLS 是否在 namesFromJson 中:
我的问题是当它为真时,我如何更新 jsonfile?
Python code:
import xlrd
import unicodedata
import json
intents_file = open("C:\myJsonFile.json","rU")
json_intents_data = json.load(intents_file)
book = xlrd.open_workbook("C:\myXLSFile.xlsx")
sheet = book.sheet_by_index(0)
row =""
nameXlsValues = []
intentJsonNames =[]
for entity in json_intents_data["intents"]:
intentJsonName = entity["name"]
intentJsonNames.append(intentJsonName)
for row_index in xrange(sheet.nrows):
nameXlsValue = sheet.cell(rowx = row_index,colx=0).value
nameXlsValues.append(nameXlsValue)
if nameXlsValue in intentJsonNames:
#here ,I have to extract row values from xlsFile and update jsonFile
for col_index in xrange(sheet.ncols):
value = sheet.cell(rowx = row_index,colx=col_index).value
if type(value) is unicode:
value = unicodedata.normalize('NFKD',value).encode('ascii','ignore')
row += "{0} - ".format(value)
my json file is like this :
{
"intents": [
{
"id": "id1",
"name": "name1",
"details": {
"tags": [
"tag1"
],
"answers": [
{
"type": "switch",
"cases": [
{
"case": "case1",
"answers": [
{
"tips": [
""
],
"texts": [
"my text to be updated"
]
}
]
},
{
"case": "case2",
"answers": [
{
"tips": [
"tip2"
],
"texts": [
]
}
]
}
]
}
],
"template": "json",
"sentences": [
"sentence1",
" sentence2",
" sentence44"]
}
},
{
"id": "id2",
"name": "name3",
"details": {
"tags": [
"tag2"
],
"answers": [
{
"type": "switch",
"cases": [
{
"case": "case1",
"answers": [
{
"texts": [
""
]
}
]
},
{
"case": "case2",
"answers": [
{
"texts": [
""
]
}
]
}
]
}
],
"sentences": [
"sentence44",
"sentence2"
]
}
}
]
}
我的 xls 文件是这样的:
[![enter image description here][1]][1]
【问题讨论】:
-
你能告诉我们一些来自 json 和 xlxs 的内容吗?究竟是什么行不通?你也在用python2.7吗?
-
我的问题是如何更新json文件(当nameFromXls在namesFromJson中时)
-
请把它放在问题中而不是 cmets,我们还需要来自 xlxs 的示例数据。
-
XLS数据文件可以转成Json格式吗?
-
https://pypi.org/project/excel2json-3/。虽然我建议切换到 python 3,因为 python 2 将不再受支持。