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?

Excelを使わずに巨大CSVを抜粋する

0
Last updated at Posted at 2026-07-03

きっかけ

10万行以上のCSVファイルを、Excelで開くのは大変です。
先頭3行以外のデータを削って、Excelで見られるようにしてほしい、
と依頼されたときの対応方法です。

データ元のファイルは target.csv 文字コードが Shift-JIS
新しく保存するファイルは result.csv だったとします

Get-Content .\target.csv -encoding Shift_jis| Select-Object -First 4 | Set-Content result.csv -Encoding shift_jis

Set-Content -Encoding shift_jis を付けています。これを付けないと、文字コードがUTF8で保存されてしまい、Excelで開いた時に文字化けします。

Import-Csv でも同様の処理は可能です。Import-Csv を少し試してみましたが、CSVは文字列ではなくオブジェクトとして読み込まれます。

そのため、画面に表示すると1レコードずつ縦方向に展開され、CSV本来の見た目で確認しづらくなります。
元のCSV形式のまま表示するには、追加の書式指定が必要でした。

まとめ

データの集計や、複雑な編集加工を行うなら Import-Csv を使い便利さを感じられるが、表示だけなら、Get-Content にしておいたほうが良さそう。

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?