0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Papa ParseでCSVを解析する

Posted at

概要

JavaScriptでCSVを読み込む場合、自力を実装するよりはPapa Parseのようなライブラリを利用したほうが色々ラクです。

Papa Parseを利用するメリット

  • シンプルで使いやすい
  • RFC 4180に基づいている
  • 区切り文字は自動検知
  • 改行やクォーテーションを正しく処理してくれる

ネットファイルをダウンロードして読み込む例

const res = await fetch('http://localhost/test.csv');
if (!res.ok) {
  throw new Error(`HTTP error! status: ${res.status}`);
}

const csvText = await res.text();

Papa.parse<CsvData>(csvText, {
  header: true, // ヘッダ行あり
  skipEmptyLines: true, // 空行を無視
  complete: ({ data }) => {
    setCsvData(data);
  },
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?