如何在将数据保存为JSON之前添加自定义字段

huangapple go评论66阅读模式
英文:

how to add customized fields before saving the data to json

问题

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook

workbook = Workbook("sample.xlsx")
workbook.save("sample3.json")
jpype.shutdownJVM()

输出如下所示:

[
 {
  "Name": "DS",
  "Gender": "M"
 },
{
  "Name": "DS1",
  "Gender": "M"
 },
]

我想要输出中添加一些额外(硬编码)字段和单词数据,如下所示:

[
 {
  "date": "06/05/2022",
  "data":{
    "Name": "DS",
    "Gender": "M"
    }
 },
 {
  "date": "06/05/2022",
  "data":{
   "Name": "DS1",
   "Gender": "M"
  }
 },
]

关于Aspose Cells,我参考了Aspose文档,但没有找到如何执行此操作的方式。感谢您的提前帮助。

英文:

I have a simple python program which converts excel to json .

import  jpype     
import  asposecells     
jpype.startJVM() 
from asposecells.api import Workbook
workbook = Workbook("sample.xlsx")
workbook.save("sample3.json")
jpype.shutdownJVM()

And i get the output as below

[
 {
  "Name": "DS",
  "Gender": "M"
 },
{
  "Name": "DS1",
  "Gender": "M"
 },
]

Instead i want the output to have some extra (hardcoded) fields and word data appended

[
 {
  "date": "06/05/2022",
  "data":{
    "Name": "DS",
    "Gender": "M"
    }
 },
 {
  "date": "06/05/2022",
  "data":{
   "Name": "DS1",
   "Gender": "M"
  }
 },
]

Any insights on aspose-cells ? i referred the aspose docs , but didnt find anyways to do it. Any help will be appreciated. Thanks in advance.

答案1

得分: 1

将其转换为Python对象,修改后保存,而不是保存为JSON。如果您无法使用该库创建JSON字符串/Python对象,可以使用JSON库加载该文件并进行必要的更改:

import json

# 加载JSON文件
with open('sample3.json', 'r') as infile:
    json_list = json.load(infile)

new_list = []

for item in json_list:
    # 进行所需的修改,例如...
    new_list.append({"date": "1-1-2020", "data": item})

# 保存修改后的数据为JSON文件
with open('sample3_extended.json', 'w') as outfile:
    json.dump(new_list, outfile, indent=2)

您硬编码示例的格式有问题,我猜您想要一个类似这样的字典列表:

[
 {
  "date": "06/05/2022",
  "data": {
    "Name": "DS",
    "Gender": "M"
  }
 },
 {
  "date": "06/05/2022",
  "data": {
   "Name": "DS1",
   "Gender": "M"
  }
 }
]
英文:

Instead of saving it to a json, convert it to a python object, modify it and save. If you cannot use that library to create a json string/python object, you can load that file with the json library and make the necessary changes:

import json
json_list = json.load("sample3.json")
new_list = []

for l in json_list:
    #do_whatever, for example...
    new_list.append({"data": l, "date": "1-1-2020"})

with open('sample3_extended.json', 'w') as outfile:
    json.dump(new_list, outfile)

The formatting of your hardcoded example is wrong, I guess you want a list of dictionaries like this:

[
 {
  "date": "06/05/2022",
  "data":{
    "Name": "DS",
    "Gender": "M"
    }
 },
 {
  "date": "06/05/2022",
  "data":{
   "Name": "DS1",
   "Gender": "M"
  }
 },
]

答案2

得分: 0

我建议使用我另一个答案中的方法(这就是为什么我发布了一个新答案而不是编辑),加载JSON文件并在列表中进行迭代。

然而,如果您需要每行都有100%“独立”的JSON字典,您可以将信息保存为txt文件,按照JSON标准每行添加一个字典。

然后,您可以一次读取一行,它将与JSON兼容(取决于您如何使用它,您可能需要去掉\n,将字符串转换为JSON(在Python中使用json.loads(my_dict_string)等)。

要创建txt文件:

import json
json_list = json.load("sample3.json")
new_list = []

for l in json_list:
    #do_whatever, for example...
    my_dict = {"data": l, "date": "1-1-2020"}
    new_list.append(json.dumps(my_dict))

with open('sample3_extended.txt', 'w') as outfile:
    outfile.write('\n'.join(new_list))

这将创建一个如下所示的txt文件:

{"data": 1, "date": "1-1-2020"}
{"data": 2, "date": "1-1-2020"}
{"data": 3, "date": "1-1-2020"}
{"data": 4, "date": "1-1-2020"}
英文:

I would recommend to use the approach from my other answer (that is why I post a new answer instead of editing), load the json file and iterate in the list.

However, if you need 100% "independent" json dicts per line, you can save the info as a txt file, adding one dict per line following json standards.

Then you can read one line at a time and it will be json compatible (depending on how you use it you will need to get rid of the \n, transform the string to json (json.loads(my_dict_string) in Python, etc.

To create the txt file:

import json
json_list = json.load("sample3.json")
new_list = []

for l in json_list:
    #do_whatever, for example...
    my_dict = {"data": l, "date": "1-1-2020"}
    new_list.append(json.dumps(my_dict))


with open('sample3_extended.txt', 'w') as outfile:
    outfile.write('\n'.join(new_list))

This will create a txt file like this:

# json_list = [1,2,3,4]
{"data": 1, "date": "1-1-2020"}
{"data": 2, "date": "1-1-2020"}
{"data": 3, "date": "1-1-2020"}
{"data": 4, "date": "1-1-2020"}

huangapple
  • 本文由 发表于 2023年6月6日 02:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定