Remove leading zeros in csv file from int values only

前端 未结 1 1521
我在风中等你
我在风中等你 2021-01-21 18:02

I have this csv file I\'m trying to remove leading zeros from, but with my code for any numbers that have letters or special characters, it nulls out the value, and I don\'t wan

相关标签:
1条回答
  • 2021-01-21 18:28

    Try using ".TrimStart()" to take out the zeros.

    Import-CSV "C:\path\test.csv" | ForEach-Object{
        $_.column1 = ($_.column1).TrimStart('0')
        $_
    } | Export-Csv "C:\path\test2.csv" -NoTypeInformation
    Move-Item "C:\path\test2.csv" "C:\path\test.csv" -Force
    

    Note that in order for this to work, I had to reformat your first column to take out the extra spaces. Thus, the header in the original csv would start with: column1,column2, and so on.

    0 讨论(0)
提交回复
热议问题