{"id":2244,"date":"2021-12-09T20:46:55","date_gmt":"2021-12-09T11:46:55","guid":{"rendered":"https:\/\/dennie.tokyo\/it\/?p=2244"},"modified":"2025-12-07T14:42:17","modified_gmt":"2025-12-07T05:42:17","slug":"%e3%80%90node-js%e3%80%91csv%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e5%87%ba%e5%8a%9b","status":"publish","type":"post","link":"https:\/\/dennie.tokyo\/it\/?p=2244","title":{"rendered":"\u3010Node.js\u3011CSV\u30d5\u30a1\u30a4\u30eb\u51fa\u529b"},"content":{"rendered":"\n<p>CSV\u30d5\u30a1\u30a4\u30eb\u3092\u64cd\u4f5c\u3059\u308b\u3053\u3068\u306f\u3088\u304f\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u4eca\u56de\u306f Node.js \u3092\u4f7f\u7528\u3057\u3066CSV\u30d5\u30a1\u30a4\u30eb\u306e\u51fa\u529b\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u30e9\u30a4\u30d6\u30e9\u30ea<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">csv-stringify<\/h2>\n\n\n\n<p>\u5909\u6570\u306e\u5185\u5bb9(\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3084\u914d\u5217)\u3092CSV\u5f62\u5f0f(\u30ab\u30f3\u30de\u533a\u5207\u308a)\u3078\u5909\u63db\u3059\u308b\u306b\u306f csv-stringify \u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5229\u7528\u3059\u308b\u3068\u7c21\u5358\u306b\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>npm\u306b\u3088\u308b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u30b3\u30de\u30f3\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>npm install csv-stringify<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">iconv-lite<\/h2>\n\n\n\n<p>CSV\u30d5\u30a1\u30a4\u30eb\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u306f Shift-JIS \u3067\u6271\u3046\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u6271\u3046\u6587\u5b57\u30b3\u30fc\u30c9\u306f\u57fa\u672c\u7684\u306bUTF-8\u306a\u306e\u3067\u3001Shift-JIS \u3078\u5909\u63db\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>npm\u306b\u3088\u308b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u30b3\u30de\u30f3\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>npm install iconv-lite<\/code><\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\">CSV\u30d5\u30a1\u30a4\u30eb\u306e\u51fa\u529b<\/h1>\n\n\n\n<p>\u57fa\u672c\u7684\u306a\u51e6\u7406\u306e\u6d41\u308c\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5909\u6570\u306e\u5024\u3092 csv-stringify \u3067CSV\u30d5\u30a1\u30a4\u30eb\u5f62\u5f0f(\u30ab\u30f3\u30de\u533a\u5207\u308a)\u3078\u5909\u63db\u3059\u308b\u3002<\/li><li>iconv-lite \u3067\u6587\u5b57\u30b3\u30fc\u30c9\u3092 Shift-JIS \u3078\u5909\u63db\u3059\u308b\u3002<\/li><li>\u30d5\u30a1\u30a4\u30eb\u51fa\u529b\u3059\u308b\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">CSV\u30d5\u30a1\u30a4\u30eb\u306e\u5b9a\u7fa9<\/h2>\n\n\n\n<p>CSV\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u3092\u5b9a\u7fa9\u3059\u308b\u4e0a\u3067\u3001\u30d8\u30c3\u30c0\u30fc\u306e\u6709\u7121\u3001\u5024\u3092\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c8\u3067\u56f2\u3080\u304b\u3069\u3046\u304b\u3092\u6c7a\u3081\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002<br>\u4eca\u56de\u306f\u3001\u30d8\u30c3\u30c0\u30fc\u3042\u308a\u3001\u5024\u3092\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c8\u3067\u56f2\u307fCSV\u30d5\u30a1\u30a4\u30eb\u3092\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>const Fs  = require(&#39;fs&#39;);\nconst { stringify } = require(&quot;csv-stringify\/sync&quot;);\nconst Iconv = require(&#39;iconv-lite&#39;);\n\nlet contents = [\n    { &quot;\u5e74\u9f62&quot;: &quot;10&quot;, &quot;\u540d\u524d&quot;: &#39;\u30a4\u30c1\u30ed\u30fc&#39; },\n    { &quot;\u5e74\u9f62&quot;: &quot;20&quot;, &quot;\u540d\u524d&quot;: &#39;\u30b8\u30ed\u30fc&#39; },\n    { &quot;\u5e74\u9f62&quot;: &quot;30&quot;, &quot;\u540d\u524d&quot;: &#39;\u30b5\u30d6\u30ed\u30fc&#39; },\n];\n\n\/\/ \u30ab\u30f3\u30de\u533a\u5207\u308a\u3078\u5909\u63db\u3059\u308b\u3002\nconst csvString = stringify(contents, {\n    header: true,\n    quoted_string: true,\n});\n\n\/\/ Shift_JSI\u3078\u5909\u63db\u3059\u308b\u3002\nconst csvStringSjis = Iconv.encode(csvString, &#39;Shift_JIS&#39;);\n\n\/\/ CSV\u30d5\u30a1\u30a4\u30eb\u3092\u51fa\u529b\u3059\u308b\u3002\nFs.writeFileSync(&quot;result.txt&quot;, csvStringSjis);<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u7d50\u679c<\/h2>\n\n\n\n<p>\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>&quot;\u5e74\u9f62&quot;,&quot;\u540d\u524d&quot;\n&quot;10&quot;,&quot;\u30a4\u30c1\u30ed\u30fc&quot;\n&quot;20&quot;,&quot;\u30b8\u30ed\u30fc&quot;\n&quot;30&quot;,&quot;\u30b5\u30d6\u30ed\u30fc&quot;<\/code><\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\">\u6700\u5f8c\u306b<\/h1>\n\n\n\n<p>\u7279\u306b\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSV\u30d5\u30a1\u30a4\u30eb\u3092\u64cd\u4f5c\u3059\u308b\u3053\u3068\u306f\u3088\u304f\u3042\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":4346,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-2244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js"],"_links":{"self":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/2244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2244"}],"version-history":[{"count":6,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/2244\/revisions"}],"predecessor-version":[{"id":3296,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/2244\/revisions\/3296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/media\/4346"}],"wp:attachment":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}