node:read_csv
差分
このページの2つのバージョン間の差分を表示します。
次のリビジョン | 前のリビジョン | ||
node:read_csv [2023/12/21 11:24] – 作成 mikoto | node:read_csv [2023/12/21 12:37] (現在) – mikoto | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | [[: | ||
+ | |||
まず、** csv-parser ** モジュールをインストールする | まず、** csv-parser ** モジュールをインストールする | ||
< | < | ||
行 4: | 行 6: | ||
</ | </ | ||
- | ヘッダー付きのCSVを読み込み、id、url、value をdata 配列に格納する。 | + | ヘッダー付きのCSVを読み込み、sku、url、cprice |
<code javascript> | <code javascript> | ||
+ | /* CSVを読み込み | ||
+ | |||
+ | */ | ||
+ | |||
const fs = require(' | const fs = require(' | ||
const csv = require(' | const csv = require(' | ||
- | const data = []; | + | const filePath = ' |
+ | const inputData | ||
- | fs.createReadStream(' | + | fs.createReadStream(filePath) |
- | .pipe(csv({ | + | .pipe(csv({ |
.on(' | .on(' | ||
- | | + | const sku = row.sku; |
- | | + | |
const url = row.url; | const url = row.url; | ||
- | const value = row.value; | + | const currentPrice |
- | + | ||
- | // 例: オブジェクトとして格納 | + | // オブジェクトとして格納 |
- | | + | |
}) | }) | ||
.on(' | .on(' | ||
// CSV読み込み完了時の処理 | // CSV読み込み完了時の処理 | ||
console.log(' | console.log(' | ||
- | console.log(data); // dataにCSVのデータが格納されています | + | console.log(inputData); |
}) | }) | ||
.on(' | .on(' | ||
// エラー処理 | // エラー処理 | ||
- | console.error('Error reading CSV file:', error.message); | + | console.log('エラー:', error.message); |
}); | }); | ||
</ | </ | ||
+ | |||
+ | CSV入力時のrow.のあとの部分はCSVのヘッダーと同じにする。 | ||
+ | ここが間違っていると読み込まれない。 |
node/read_csv.1703125454.txt.gz · 最終更新: 2023/12/21 11:24 by mikoto