Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how json dump Japanese
#1
help,please.

I am coding a spider-code to collect message on a Japanese website, storing data using json-module

however, default json dump the data with Unicode. cant see raw Japanese

I see one solution on the internet is
json.loads('このコーディネートのスタイリストについて',encoding='utf-8')
but, this keyword is not available in json dump!
json.dump('このコーディネートのスタイリストについて',fp,encoding='utf-8')
this code raise an error that cant decode the 'gbk'!

but i cant encode my data structure into Unicode.
how can I see raw Japanese in my json file?
Reply
#2
import json
my_data = '{"some_japanese_text":"このコーディネートのスタイリストについて"}'

# load it in a dict
json_data = json.loads(my_data, encoding='utf-8')
print(type(json_data))

# print the sting
print(json.dumps(json_data, indent=2, ensure_ascii=False))

# or write it to foo.json
with open('foo.json', 'w') as jf:
    json.dump(json_data, jf, indent=2, ensure_ascii=False)
Output:
<class 'dict'> { "some_japanese_text": "このコーディネートのスタイリストについて" }
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
To JSON serialize Unicode or non-ASCII data as-is strings instead of \u escape sequence.

The json.dump() and json.dumps() has a ensure_ascii parameter. The ensure_ascii is by-default true so the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii=False, these characters will be output as-is.

json.dumps('このコーディネートのスタイリストについて', ensure_ascii=False)
.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  JSON Dump and JSON Load foxholenoob 8 977 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 2,355 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  Python html herf to json dump help paulfearn100 0 1,996 Mar-03-2020, 09:16 PM
Last Post: paulfearn100
  print python json dump onto multiple lines lhailey 2 19,651 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  creating hex dump modifying program ensoniq 3 2,593 Oct-14-2019, 08:21 AM
Last Post: Larz60+
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 21,687 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  Using pickle.dump Friend 1 2,901 Feb-15-2019, 04:39 PM
Last Post: metulburr
  tf.gfile.FastGFile error unicode ( japanese characters ) majinbuu 2 3,049 May-13-2018, 02:11 PM
Last Post: majinbuu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020