【问题标题】:Localization from Excel to JSON using Python使用 Python 从 Excel 本地化到 JSON
【发布时间】: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


【解决方案1】:

要将数据帧转换为 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)

【讨论】:

  • 由于区分大小写而导致 en 出现错误。还有编码问题。有些字符是 ue4..
  • 再次检查@crazyDev。您可能需要df.columns = df.columns.str.lowercase() 而不是df.iloc[0] = ...,就像我在回答中所做的那样。
  • 网站不接受我们转移链接。我需要支持像日语这样的语言,然后在编码方面遇到了困难。
  • 如果你能把文件给我,我能帮上大忙。您可以尝试其他链接,例如 Google Drive 吗?还是 GitHub?
猜你喜欢
  • 2021-02-24
  • 2021-10-13
  • 2022-11-10
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
相关资源
最近更新 更多