LoginSignup
0
1

PowerShellでcsvを読み込んで内容をいじってexportする

Posted at

以下のようなcsvがあるとして

#C:\Users\ユーザー名\Desktop\test.csv
#csvの内容↓
#date,price,result
#"2024/01/01","3,000","OK"
  • dateから"/"を取りたい
  • priceの","を取りたい
  • 文字コードをUTF8にしたい
    と思ったときは
Import-Csv -LiteralPath 'C:\Users\ユーザー名\Desktop\test.csv' -Encoding Default | Select-Object @{Name='cnv_date';Expression={$_.date -replace "/", "" }},@{Name='cnv_price';Expression={$_.price -replace ",", "" }},"result" | Export-Csv -Path "C:\Users\ユーザー名\Desktop\cnv_test.csv" -NoTypeInformation -Encoding UTF8

とすると以下のようなCSVとなる。

#C:\Users\ユーザー名\Desktop\cnv_test.csv
#csvの内容↓
#cnv_date,cnv_price,result
#"20240101","3000","OK"

(-NoTypeInformationをつけないと出力csvの1行目に不要な文字列が記録される)

0
1
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
1