Skip to content

Instantly share code, notes, and snippets.

@acokikoy
Created October 19, 2019 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acokikoy/0f379f9f08b82860b9db26549ab6d4aa to your computer and use it in GitHub Desktop.
Save acokikoy/0f379f9f08b82860b9db26549ab6d4aa to your computer and use it in GitHub Desktop.
Sample code of json.dumps() method.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict is ... <class 'dict'>\n",
"{'name': 'Yamada Taro', 'name_jp': '山田\\t太郎', 'age': 23, 'gender': 'man', 'is_member': True, 'foo': None}\n",
"******************\n",
"encode is ... <class 'str'>\n",
"'{\"name\": \"Yamada Taro\", \"name_jp\": \"\\\\u5c71\\\\u7530\\\\t\\\\u592a\\\\u90ce\", \"age\": 23, \"gender\": \"man\", \"is_member\": true, \"foo\": null}'\n",
"'{\"name\": \"Yamada Taro\", \"name_jp\": \"山田\\\\t太郎\", \"age\": 23, \"gender\": \"man\", \"is_member\": true, \"foo\": null}'\n"
]
}
],
"source": [
"# json.dumpsは何をしてくれるモノなのか\n",
"import json\n",
"\n",
"dict = {\n",
" \"name\": \"Yamada Taro\",\n",
" \"name_jp\": \"山田\\t太郎\", # エスケープ文字, 非ascii文字\n",
" \"age\": 23, # numeric\n",
" \"gender\": \"man\",\n",
" \"is_member\": True, # Boolean\n",
" \"foo\": None # None\n",
"}\n",
" \n",
"encoded = json.dumps(dict)\n",
"encoded_not_ascii = json.dumps(dict, ensure_ascii=False)\n",
"\n",
"print(f\"dict is ... {type(dict)}\")\n",
"print(repr(dict))\n",
"print(\"******************\")\n",
"print(f\"encode is ... {type(encoded)}\")\n",
"print(repr(encoded))\n",
"print(repr(encoded_not_ascii))\n"
]
}
],
"metadata": {
"gist": {
"data": {
"description": "json.dumpsは何をしてくれるモノなのか",
"public": true
},
"id": ""
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "165px"
},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment