【问题标题】:How can I print Greek Characters in Python in Terminal (Mac OSX) and not in a file如何在终端(Mac OSX)而不是文件中打印 Python 中的希腊字符
【发布时间】:2021-04-19 21:51:29
【问题描述】:

我有以下问题: 我正在尝试执行代码:

import requests
import json
url = 'https://data.gov.gr/api/v1/query/mdg_emvolio?date_from=2021-01-07&date_to=2021-01-14'
headers = {'Authorization':'Token xxxxxxxxxxxxxx’}
response = requests.get(url, headers=headers)
json_object = json.loads(response.text)
json_formatted_str = json.dumps(json_object, indent=2)
print(json_formatted_str)

我希望输出格式为 json 格式,但希腊字符显示不正确。我得到以下输出(一部分):

{
    "area": "\u0391\u0399\u03a4\u03a9\u039b\u039f\u0391\u039a\u0391\u03a1\u039d\u0391\u039d\u0399\u0391\u03a3",
    "areaid": 701,
    "daydiff": 10,
    "daytotal": 60,
    "referencedate": "2021-01-07T00:00:00",
    "totaldistinctpersons": 210,
    "totalvaccinations": 210
  },

另一方面,如果我使用:

print (response.json())

我得到了正确的希腊字符,但(如预期的那样)不好,例如以下:

{'area': 'ΑΙΤΩΛΟΑΚΑΡΝΑΝΙΑΣ', 'areaid': 701, 'daydiff': 10, 'daytotal': 60, 'referencedate': '2021-01-07T00:00:00', 'totaldistinctpersons': 210, 'totalvaccinations': 210},

有什么想法吗?

【问题讨论】:

标签: json python-3.x utf-8 character-encoding


【解决方案1】:

按照 JosefZ 的建议,我将代码修改如下:

import requests
import json
import pprint
url = 'https://data.gov.gr/api/v1/query/mdg_emvolio?date_from=2021-01-07&date_to=2021-01-14'
headers = {'Authorization':'Token xxxxxxxxxxxxxx’}
response = requests.get(url, headers=headers)
pprint.pprint(response.json()) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 2017-03-09
    • 2021-09-10
    相关资源
    最近更新 更多