使用 Python 从 Excel 本地化到 JSON
Localization from Excel to JSON using Python
我有 JSON 个不同语言的文件。我必须添加更多从 Excel 文件中读取的值。
例如,这是 Excel table:
-------------------------------
| 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"
}
}
注:en是Excel中的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,您可以将 en
设置为索引(并且仍然 保留 en
列)并致电 to_json
:
json = df.set_index('en', drop=False).to_json(indent=2)
输出:
>>> print(json)
{
"en":{
"Ball":"Ball",
"Snow":"Snow",
"Elephant":"Elephant",
"Woman":"Woman",
"Potato":"Potato",
"Tomato":"Tomato",
"Carrot":"Carrot"
},
"de":{
"Ball":"Ball",
"Snow":"Schnee",
"Elephant":"Elephant",
"Woman":"Frau",
"Potato":"Kartoffeln",
"Tomato":"F",
"Carrot":"G"
},
"ru":{
"Ball":"AA",
"Snow":"BB",
"Elephant":"CC",
"Woman":"DD",
"Potato":"EE",
"Tomato":"FF",
"Carrot":"GG"
}
}
整个脚本大概是这样的:
import json
# Load old JSON from a file.
with open('old_json.json') as f:
old_json = json.load(f)
# Load new data from spreadsheet.
new_data = pd.read_excel('...')
# Create dataframe from old JSON.
old_data = pd.DataFrame(old_json)
# Convert columns of both dataframes to lowercase.
new_data.columns = new_data.columns.astype(str).str.lower()
old_data.columns = old_data.columns.astype(str).str.lower()
# Append new data to old data and convert joined data to JSON.
new_json = pd.concat([old_data, new_data.set_index('en', drop=False)]).to_dict()
# Save new JSON to a file.
with open('new_json.json', 'w') as f:
json.dump(new_json, f)
我有 JSON 个不同语言的文件。我必须添加更多从 Excel 文件中读取的值。 例如,这是 Excel table:
-------------------------------
| 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"
}
}
注:en是Excel中的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,您可以将 en
设置为索引(并且仍然 保留 en
列)并致电 to_json
:
json = df.set_index('en', drop=False).to_json(indent=2)
输出:
>>> print(json)
{
"en":{
"Ball":"Ball",
"Snow":"Snow",
"Elephant":"Elephant",
"Woman":"Woman",
"Potato":"Potato",
"Tomato":"Tomato",
"Carrot":"Carrot"
},
"de":{
"Ball":"Ball",
"Snow":"Schnee",
"Elephant":"Elephant",
"Woman":"Frau",
"Potato":"Kartoffeln",
"Tomato":"F",
"Carrot":"G"
},
"ru":{
"Ball":"AA",
"Snow":"BB",
"Elephant":"CC",
"Woman":"DD",
"Potato":"EE",
"Tomato":"FF",
"Carrot":"GG"
}
}
整个脚本大概是这样的:
import json
# Load old JSON from a file.
with open('old_json.json') as f:
old_json = json.load(f)
# Load new data from spreadsheet.
new_data = pd.read_excel('...')
# Create dataframe from old JSON.
old_data = pd.DataFrame(old_json)
# Convert columns of both dataframes to lowercase.
new_data.columns = new_data.columns.astype(str).str.lower()
old_data.columns = old_data.columns.astype(str).str.lower()
# Append new data to old data and convert joined data to JSON.
new_json = pd.concat([old_data, new_data.set_index('en', drop=False)]).to_dict()
# Save new JSON to a file.
with open('new_json.json', 'w') as f:
json.dump(new_json, f)