【发布时间】:2022-01-21 04:46:18
【问题描述】:
我有不同语言的 JSON 文件。我必须添加从 Excel 文件中读取的更多值。 例如,这是 Excel 表格:
-------------------------------
| EN | DE | RU |
------------+------------+-----
| Ball | Ball | AA |
| Snow | Schnee | BB |
| Elephant | Elephant | CC |
| Woman | Frau | DD |
| Potato | Kartoffeln | EE |
| Tomato | F | FF |
| Carrot | G | GG |
-------------------------------
我应该在其中添加这些值的 JSON 文件:
{
"en": {
"Ball": "Ball",
"Snow": "Snow"
},
"de": {
"Ball": "Ball",
"Snow": "Schnee"
},
"ru": {
"Ball": "AA",
"Snow": "BB"
}
}
注意:在 Excel 中 en 是 EN。 JSON 键必须与英文值完全相同。 试过了,还是不行:
# Importing dependencies
import pandas
import json
# Reading xlsx into pandas dataframe
df = pandas.read_excel('Translations.xlsx')
df.iloc[0] = df.iloc[0].str.lower()
jsonfile = df.set_index('en', drop=False).to_json(indent=2)
# Encoding/decoding a Dataframe using 'columns' formatted JSON
jsonfile = df.to_json(orient='columns')
# Print out the result
print('Excel Sheet to JSON:\n', jsonfile)
# Make the string into a list to be able to input in to a JSON-file
json_dict = json.loads(jsonfile)
# write from and file to write to
with open('Localization.json', 'w', encoding='utf-8') as json_file:
json.dump(json_dict, json_file)
【问题讨论】:
-
那么问题是什么?您是否尝试将 JSON 转换为上表?
-
不,我想将 Excel 转换为 JSON 并将其添加到现有的 JSON 文件中。想象一下,我必须将“Snow”下面的所有值从 excel 添加到 JSON。
-
想象一下,我必须将“Snow”下面的所有值从 excel 添加到 JSON。我的同事翻译我的文本并将它们发送到 Excel 中,如问题所示。我的任务是创建脚本,将它们添加到现有的 JSON 文件中。
-
那么将数据添加到 JSON 文件中?
-
我知道如何阅读它,但在编码方面遇到了困难,因为它无法正确编码某些字母。另外,我是新手,还在学习Python。 :)
标签: python json excel pandas localization