Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active March 9, 2017 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/faeb5ad760fc6097c0eac938048c58f6 to your computer and use it in GitHub Desktop.
Save stknohg/faeb5ad760fc6097c0eac938048c58f6 to your computer and use it in GitHub Desktop.
例のCSVをアレするアレ
# 面倒なので適当
"http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv" `
| % { (curl $_).Content } `
| % { [Text.Encoding]::GetEncoding(932).GetString([Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($_)) -split "`r`n" } `
| ? { $_ -ne "" -and $_ -notlike "*,,,,," } `
| select -Skip 2 `
| % { $cs = $_ -split ','; for( $i=0; $i -lt $cs.Length; $i+=2 ){ [PSCustomObject]@{Date=[Datetime]$cs[$i+1];Name=$cs[$i]} } } `
| sort Date `
| ConvertTo-Csv -NoTypeInformation
# ConvertFrom-Csv使った版
"http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv" `
| % { (curl $_).Content } `
| % { [Text.Encoding]::GetEncoding(932).GetString([Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($_)) -split "`r`n" } `
| ? { $_ -ne "" -and $_ -notlike "*,,,,," } `
| select -Skip 2 `
| ConvertFrom-Csv -Header (1..3|%{"N$_","D$_"}) -PipelineVariable R `
| % { 1..3 | % { [PSCustomObject]@{ Date=[Datetime]$R.$("D$_"); Name=$R.$("N$_")} } } `
| sort Date `
| ConvertTo-Csv -NoTypeInformation
@stknohg
Copy link
Author

stknohg commented Feb 22, 2017

結果

PS C:\> "http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv" `
>>   | % { (curl $_).Content } `
>>   | % { [Text.Encoding]::GetEncoding(932).GetString([Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($_)) -split "`r`n" } `
>>   | ? { $_ -ne "" -and $_ -notlike "*,,,,," } `
>>   | select -Skip 2 `
>>   | % { $cs = $_ -split ','; for( $i=0; $i -lt $cs.Length; $i+=2 ){ [PSCustomObject]@{Date=[Datetime]$cs[$i+1];Name=$cs[$i]} } } `
>>   | sort Date `
>>   | ConvertTo-Csv -NoTypeInformation
"Date","Name"
"2016/01/01 0:00:00","元日"
"2016/01/11 0:00:00","成人の日"
"2016/02/11 0:00:00","建国記念の日"
"2016/03/20 0:00:00","春分の日"
"2016/04/29 0:00:00","昭和の日"
"2016/05/03 0:00:00","憲法記念日"
"2016/05/04 0:00:00","みどりの日"
"2016/05/05 0:00:00","こどもの日"
"2016/07/18 0:00:00","海の日"
"2016/08/11 0:00:00","山の日"
"2016/09/19 0:00:00","敬老の日"
"2016/09/22 0:00:00","秋分の日"
"2016/10/10 0:00:00","体育の日"
"2016/11/03 0:00:00","文化の日"
"2016/11/23 0:00:00","勤労感謝の日"
"2016/12/23 0:00:00","天皇誕生日"
"2017/01/01 0:00:00","元日"
"2017/01/09 0:00:00","成人の日"
"2017/02/11 0:00:00","建国記念の日"
"2017/03/20 0:00:00","春分の日"
"2017/04/29 0:00:00","昭和の日"
"2017/05/03 0:00:00","憲法記念日"
"2017/05/04 0:00:00","みどりの日"
"2017/05/05 0:00:00","こどもの日"
"2017/07/17 0:00:00","海の日"
"2017/08/11 0:00:00","山の日"
"2017/09/18 0:00:00","敬老の日"
"2017/09/23 0:00:00","秋分の日"
"2017/10/09 0:00:00","体育の日"
"2017/11/03 0:00:00","文化の日"
"2017/11/23 0:00:00","勤労感謝の日"
"2017/12/23 0:00:00","天皇誕生日"
"2018/01/01 0:00:00","元日"
"2018/01/08 0:00:00","成人の日"
"2018/02/11 0:00:00","建国記念の日"
"2018/03/21 0:00:00","春分の日"
"2018/04/29 0:00:00","昭和の日"
"2018/05/03 0:00:00","憲法記念日"
"2018/05/04 0:00:00","みどりの日"
"2018/05/05 0:00:00","こどもの日"
"2018/07/16 0:00:00","海の日"
"2018/08/11 0:00:00","山の日"
"2018/09/17 0:00:00","敬老の日"
"2018/09/23 0:00:00","秋分の日"
"2018/10/08 0:00:00","体育の日"
"2018/11/03 0:00:00","文化の日"
"2018/11/23 0:00:00","勤労感謝の日"
"2018/12/23 0:00:00","天皇誕生日"

@stknohg
Copy link
Author

stknohg commented Mar 9, 2017

例のCSVが割と普通のCSVに変わったのでコレはもうお役御免です(:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment