{"id":328,"date":"2019-11-17T17:37:58","date_gmt":"2019-11-17T08:37:58","guid":{"rendered":"http:\/\/snow-cat.net\/?p=328"},"modified":"2021-06-10T13:36:02","modified_gmt":"2021-06-10T04:36:02","slug":"python%e3%81%a7%e6%96%87%e5%ad%97%e5%88%97%e3%82%92byte%e6%95%b0%e3%81%a7%e5%88%87%e3%82%8a%e5%87%ba%e3%81%99%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/snow-cat.net\/?p=328","title":{"rendered":"python\u3067\u6587\u5b57\u5217\u3092byte\u6570\u3067\u5207\u308a\u51fa\u3059\u65b9\u6cd5"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u3086\u304d\u306d\u3053\u3067\u3059\u3002<\/p>\n<p>\u793e\u5185\u3067\u8272\u3005\u3068\u3054\u305f\u3054\u305f\u304c\u3042\u3063\u3066\u66f4\u65b0\u304c\u6ede\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u306e\u5148\u66f4\u65b0\u306e\u30da\u30fc\u30b9\u306f\u843d\u3061\u305d\u3046\u3067\u3059\u304c\u3001\u306a\u3093\u3068\u304b\u66f4\u65b0\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3055\u3066\u3001\u30ed\u30fc\u30de\u5b57\u3068\u65e5\u672c\u8a9e\u304c\u6df7\u3058\u3063\u305f\u6587\u5b57\u5217\u304b\u3089\u4f55\u6587\u5b57\u5207\u308a\u51fa\u3057\u305f\u3044\u3063\u3066\u3053\u3068\u3042\u308a\u307e\u3059\u3088\u306d\u3002<\/p>\n<p>\u4ee5\u524d\u306b\u534a\u89d2\u30921byte\u3001\u5168\u89d2\u30922byte\u3068\u3057\u3066\u8a08\u7b97\u3057\u3066\u6c7a\u307e\u3063\u305f\u7bc4\u56f2\u306ebyte\u6570\u3067\u6587\u5b57\u3092\u5207\u308a\u51fa\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u305d\u306e\u65b9\u6cd5\u3092\u8cb7\u3044\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u8272\u3005\u3068\u30e9\u30a4\u30d6\u30e9\u30ea\u63a2\u3057\u3066\u307f\u305f\u3093\u3067\u3059\u3051\u3069\u3001\u6b32\u3057\u3044\u306e\u304c\u898b\u5f53\u305f\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u30d9\u30bf\u66f8\u304d\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3044\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u3063\u305f\u3089\u6559\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u3055\u3066\u3001\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"theme:light-abite lang:python decode:true \" title=\"trim_text_by_bytes\">import unicodedata\r\n\r\ntext=\"sample\u30c6\u30ad\u30b9\u30c8\u3067\u3059\"\r\nstrlen=40\r\nCHARACTER_TYPE=[\"H\",\"Na\"]\r\ntext_trimmed = text.strip().replace('\\n','').replace(' ','')\r\n# \u6587\u5b57\u6570\u30c1\u30a7\u30c3\u30af\r\nif len(text_trimmed) &lt;= 0:\r\n  return text_trimmed\r\n\r\npurpose_str = \"\"\r\nbyte_cnt = 0\r\n# \u4e00\u5b57\u3065\u3064\u53d6\u308a\u51fa\u3057\u3066\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\r\nfor character in text_trimmed:\r\n  if unicodedata.east_asian_width(character) in CHARACTER_TYPE:\r\n    byte_cnt += 1\r\n  else:\r\n    byte_cnt += 2\r\n  if byte_cnt &gt; strlen:\r\n    break\r\n  purpose_str += character\r\nprint(purpose_str)<\/pre>\n<p>\u7c21\u5358\u306b\u8aac\u660e\u3059\u308b\u3068\u6587\u5b57\u5217\u3092for\u6587\u3067\u56de\u3057\u3066\u534a\u89d2\u304b\u5168\u89d2\u304b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u30ab\u30a6\u30f3\u30c8\u3057\u3066\u3044\u304f\u3068\u3044\u3046\u30b7\u30f3\u30d7\u30eb\u306a\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>unicodedata\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u3066\u534a\u89d2\u304b\u5168\u89d2\u304b\u30c1\u30a7\u30c3\u30af\u3057\u3066byte\u6570\u306e\u8a08\u7b97\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3069\u306e\u3088\u3046\u306b\u5206\u985e\u3055\u308c\u308b\u304b\u306fwikipedia\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u3067\u3059\u3002<\/p>\n<p>\u4ee5\u4e0a\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u3086\u304d\u306d\u3053\u3067\u3059\u3002 \u793e\u5185\u3067\u8272\u3005\u3068\u3054\u305f\u3054\u305f\u304c\u3042\u3063\u3066\u66f4\u65b0\u304c\u6ede\u3063\u3066\u3044\u307e\u3057\u305f\u3002 \u3053\u306e\u5148\u66f4\u65b0\u306e\u30da\u30fc\u30b9\u306f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-328","post","type-post","status-publish","format-standard","hentry","category-1"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pattVc-5i","jetpack-related-posts":[{"id":137,"url":"https:\/\/snow-cat.net\/?p=137","url_meta":{"origin":328,"position":0},"title":"DynamoDB\u306e\u30e6\u30fc\u30b6\u60c5\u5831","author":"hiro","date":"2019\u5e741\u670821\u65e5","format":false,"excerpt":"\u3053\u3093\u306b\u3061\u306f\u3001\u3086\u304d\u306d\u3053\u3067\u3059\u3002 \u8ee2\u8077\u3057\u3066\u304b\u3089\u57fa\u672c\u7684\u306b\u30d5\u30ed\u30f3\u30c8\u3092\u3084\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u624b\u304c\u4f59\u3063\u3066\u30b5\u30fc\u30d0\u30b5\u30a4\u2026","rel":"","context":"AWS","block_context":{"text":"AWS","link":"https:\/\/snow-cat.net\/?cat=16"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1273,"url":"https:\/\/snow-cat.net\/?p=1273","url_meta":{"origin":328,"position":1},"title":"Stripe\u306e\u6a5f\u80fd","author":"hiro","date":"2021\u5e7411\u670819\u65e5","format":false,"excerpt":"Stripe\u4f7f\u3044\u305f\u3044\u306a\u3068\u601d\u3063\u3066\u306f\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u8272\u3005\u6a5f\u80fd\u304c\u3042\u3063\u3066\u5168\u4f53\u7684\u306b\u628a\u63e1\u3059\u308b\u306e\u304c\u3081\u3093\u3069\u304f\u3055\u3044\u306a\u3068\u601d\u2026","rel":"","context":"\u958b\u767a","block_context":{"text":"\u958b\u767a","link":"https:\/\/snow-cat.net\/?cat=9"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":403,"url":"https:\/\/snow-cat.net\/?p=403","url_meta":{"origin":328,"position":2},"title":"Django\u5b66\u7fd2\u306e\u30b9\u30b9\u30e1","author":"hiro","date":"2020\u5e744\u67081\u65e5","format":false,"excerpt":"\u3053\u3093\u306b\u3061\u306f\u3001\u3086\u304d\u306d\u3053\u3067\u3059\u3002 \u9000\u8077\u3057\u3066\u304b\u3089\u3057\u3070\u3089\u304fDjango\u3092\u3044\u3058\u3063\u3066\u305f\u306e\u3067\u3001\u3069\u3093\u306a\u5b66\u7fd2\u306e\u9032\u3081\u65b9\u304c\u3044\u2026","rel":"","context":"Python","block_context":{"text":"Python","link":"https:\/\/snow-cat.net\/?cat=18"},"img":{"alt_text":"","src":"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=snowcat043-22&language=ja_JP&l=li3&o=9&a=B07NG9BCKG","width":350,"height":200},"classes":[]},{"id":1216,"url":"https:\/\/snow-cat.net\/?p=1216","url_meta":{"origin":328,"position":3},"title":"PHP\u3067\u9023\u60f3\u914d\u5217\u3092\u6301\u3064\u914d\u5217\u304b\u3089\u8a72\u5f53\u3059\u308b\u5024\u3092\u6301\u3064\u9023\u60f3\u914d\u5217\u3092\u53d6\u5f97\u3057\u305f\u3044","author":"hiro","date":"2021\u5e7410\u670814\u65e5","format":false,"excerpt":"\u914d\u5217\u304b\u3089\u5024\u3092\u6761\u4ef6\u306b\u5f53\u3066\u306f\u307e\u308b\u914d\u5217\u3092\u53d6\u5f97\u3057\u305f\u3044\u6642\u306b\u3044\u3044\u611f\u3058\u306e\u95a2\u6570\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u95a2\u6570\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u5bfe\u8c61\u2026","rel":"","context":"PHP","block_context":{"text":"PHP","link":"https:\/\/snow-cat.net\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":447,"url":"https:\/\/snow-cat.net\/?p=447","url_meta":{"origin":328,"position":4},"title":"\u30dd\u30fc\u30c8\u30d5\u30a9\u30ea\u30aa\u3067\u4f7f\u7528\u3057\u305f\u6280\u8853","author":"hiro","date":"2020\u5e744\u670816\u65e5","format":false,"excerpt":"\u6628\u65e5\u30dd\u30fc\u30c8\u30d5\u30a9\u30ea\u30aa\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002 \u73fe\u5728\u8ee2\u8077\u4e2d\u306a\u306e\u3067\u4f5c\u3063\u305f\u306e\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u3082\u3068\u3082\u3068SPA\u3092\u4f5c\u308a\u305f\u304b\u3063\u2026","rel":"","context":"\u305d\u306e\u4ed6\u3082\u308d\u3082\u308d","block_context":{"text":"\u305d\u306e\u4ed6\u3082\u308d\u3082\u308d","link":"https:\/\/snow-cat.net\/?cat=1"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":547,"url":"https:\/\/snow-cat.net\/?p=547","url_meta":{"origin":328,"position":5},"title":"swagger\u306e\u74b0\u5883\u69cb\u7bc9","author":"hiro","date":"2020\u5e746\u67086\u65e5","format":false,"excerpt":"swagger\u306e\u30aa\u30f3\u30e9\u30a4\u30f3\u52c9\u5f37\u4f1a\u306b\u53c2\u52a0\u3057\u305f\u306e\u3067\u3001swagger\u306e\u8a2d\u5b9a\u3092\u53cd\u6620\u3057\u305f\u30e2\u30c3\u30af\u30b5\u30fc\u30d0\u3092\u7acb\u3066\u3066\u307f\u2026","rel":"","context":"\u6280\u8853","block_context":{"text":"\u6280\u8853","link":"https:\/\/snow-cat.net\/?cat=7"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/snow-cat.net\/wp-content\/uploads\/2020\/06\/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2020-06-06-16.57.38-3-300x25.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/snow-cat.net\/index.php?rest_route=\/wp\/v2\/posts\/328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snow-cat.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snow-cat.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snow-cat.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snow-cat.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=328"}],"version-history":[{"count":5,"href":"https:\/\/snow-cat.net\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions"}],"predecessor-version":[{"id":1052,"href":"https:\/\/snow-cat.net\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions\/1052"}],"wp:attachment":[{"href":"https:\/\/snow-cat.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snow-cat.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snow-cat.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}