4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

APIで取得したJSONデータの日本語文字化けを解消する。

Posted at

##発生した問題
youtube Data APIでデータを取得する練習をしていたら、取得したデータを出力すると日本語が文字化けしてた。

search.py
if __name__ == "__main__":
    try:
        response = youtube_search(
            q='公式', part='snippet', type='video', max_count=1, order='date')

        CURRENT_DIR = os.getcwd()
        with codecs.open(CURRENT_DIR + '/' + 'data.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(response, indent=2))

        print(json.dumps(response, indent=2))

    except HttpError as e:
        print("An HTTP error %d occurred: \n%s" % (e.resp.status, e.content))
結果.
########################
## 関係ないところは省略
########################
        "title": "\u4eee\u9762\u30e9\u30a4\u30c0\u30fc\uff08\u65b0\uff09\uff08\u30b9\u30ab\u30a4\u30e9\u30a4\u30c0\u30fc\uff09\u3000\u7b2c14\u8a71[\u516c\u5f0f]",
        "description": "\u4eee\u9762\u30e9\u30a4\u30c0\u30fc\uff08\u65b0\uff09 \u7b2c14\u8a71\u300c\u30cf\u30a8\u30b8\u30b4\u30af\u30b8\u30f3 \u4eee\u9762\u30e9\u30a4\u30c0\u30fc\u5371\u6a5f\u4e00\u9aea\u300d \u6d0b\u306f\u5bcc\u58eb\u6025\u30cf\u30a4\u30e9\u30f3\u30c9\u3067\u3001\u5148\u8f29\u30fb\u8c37\u6e90\u6b21\u90ce\u3068\u518d\u4f1a\u3002\u3060\u304c\u3001\u3053\u306e\u5730\u4e0b\u306b\u306f\u3001\u30cd\u30aa\u30b7\u30e7\u30c3\u30ab\u30fc\u306e\u602a\u4eba\u30cf\u30a8\u30b8\u30b4\u30af ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/yTFJ-6NnUNY/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url"

##解消方法
json.dumps()のオプション「ensure_ascii」がデフォルトでTrue、つまりASCII文字を含む場合は自動的にエスケープする処理を実行する設定がデフォルトになっている。
これをFalseに設定してエスケープされないようにする。

search.py
if __name__ == "__main__":
    try:
        response = youtube_search(
            q='公式', part='snippet', type='video', max_count=1, order='date')

        CURRENT_DIR = os.getcwd()
        with codecs.open(CURRENT_DIR + '/' + 'data.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(response, indent=2, ensure_ascii=False))

        print(json.dumps(response, indent=2, ensure_ascii=False))

    except HttpError as e:
        print("An HTTP error %d occurred: \n%s" % (e.resp.status, e.content))
結果.
########################
## 関係ないところは省略
########################
        "title": "仮面ライダー(新)(スカイライダー) 第14話[公式]",
        "description": "仮面ライダー(新) 第14話「ハエジゴクジン 仮面ライダー危機一髪」 洋は富士急ハイランドで、先輩・谷源次郎と再会。だが、この地下には、ネオショッカーの怪人ハエジゴク ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/yTFJ-6NnUNY/default.jpg",
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?