【问题标题】:Update a json file from XLS file data从 XLS 文件数据更新 json 文件
【发布时间】:2019-05-12 14:49:09
【问题描述】:

我正在尝试从 XLS 文件数据更新 json 文件。 这是我想做的:

  • 从Json 中提取名称
  • 从 XLS 中提取名称
  • 对于 namesFromXLS 中的 nameFromXLS:
    • 检查 nameFromXLS 是否在 namesFromJson 中:
      • 如果为真:那么:
        • 提取 xls 行(同名)
        • 更新 jsonFile(同名)

我的问题是当它为真时,我如何更新 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 将不再受支持。

标签: python json xls


【解决方案1】:

当您将 json 数据从文件加载到内存中时,它会变成一个名为“json_intents_data”的 python dict。

当条件 'if nameXlsValue in intentJsonNames' 为 True 时,您需要使用从 Excel 读取的数据更新 dict。 (看起来你知道怎么做。)

当循环 'for row_index in xrange(sheet.nrows):' 完成后,您的 dict 会更新,您希望将其保存为 json 文件。

import json

with open('updated_data.json', 'w') as fp:
    json.dump(json_intents_data, fp) 

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 2015-06-30
    • 2023-03-15
    • 2020-05-29
    • 2015-08-05
    • 2016-12-06
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多