仅从 int 值中删除 csv 文件中的前导零 [英] Remove leading zeros in csv file from int values only

查看:52
本文介绍了仅从 int 值中删除 csv 文件中的前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 csv 文件,我试图从中删除前导零,但是对于任何包含字母或特殊字符的数字,我的代码都会将值清零,我不希望那样......

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 want that...

column1                        ,column2                            ,column3   ,column4       ,column5,column6,column7,column8 ,column9,column10,column11,column12,column13 ,column14,column15,column16
0000013                        ,                                   ,ShopKeep  ,Store         ,       ,       ,       ,        ,       ,1034627 ,        ,        ,         ,        ,        ,
0000032                        ,                                   ,Test411   ,User411       ,0007643,       ,       ,        ,       ,1034632 ,        ,        ,         ,        ,        ,
0000142                        ,                                   ,Pinky     ,Brain         ,0571253,       ,       ,        ,J      ,1234    ,        ,        ,         ,        ,        ,
0000145                        ,Leo@ninjaturtles.com               ,Leo       ,Turtle        ,0661203,       ,       ,        ,J      ,2345    ,        ,        ,RNixon   ,        ,        ,
0000264                        ,                                   ,Richard   ,Nixon         ,0311247,       ,       ,        ,J      ,3456    ,        ,        ,         ,        ,        ,
0000291                        ,asdfasd.Random@random.com          ,asdfasd   ,Random        ,0841212,       ,       ,        ,J      ,4567    ,        ,        ,aRandom  ,        ,        ,
8984207                        ,gfdgssg.Random@random.com          ,gfdgssg   ,Random        ,0883461,p209557,       ,20160531,J      ,        ,        ,        ,         ,        ,        ,
8984208                        ,hgdhdfd.Random@random.com          ,hgdhdfd   ,Random        ,0883461,p209558,       ,20151202,J      ,        ,P209558 ,        ,         ,        ,        ,
8984209                        ,blankity.Random@random.com         ,          ,Random        ,0883461,p209559,       ,20160531,J      ,        ,P209559 ,        ,         ,        ,        ,
8984210                        ,zxcvzvc.Random@random.com          ,zxcvzvc   ,Random        ,0883461,p209561,       ,20151202,J      ,        ,        ,        ,         ,        ,        ,
231716352                      ,asdf-Random@random.com             ,asdf      ,Random        ,       ,       ,       ,        ,       ,5678    ,        ,        ,         ,        ,        ,
0118969DEL20151007             ,                                   ,asdfg     ,Random        ,       ,s051699,       ,        ,J      ,6789    ,        ,        ,         ,        ,        ,
0472549DEL20151021             ,asdfgh.Random@random.com           ,asdfgh    ,Random        ,1565030,sB597RQ,       ,        ,J      ,7890    ,        ,        ,         ,        ,        ,
0812310DEL111115               ,asdfghj.Random@random.com          ,asdfghj   ,Random        ,1562482,s052177,       ,        ,J      ,0987    ,        ,        ,         ,        ,        ,
0864549DEL20151029             ,                                   ,asdfghjs  ,Random        ,0561118,s051645,       ,20150322,J      ,9867    ,        ,        ,         ,        ,        ,
0895960DEL20151007             ,                                   ,lkjh      ,Random        ,       ,s85GRHZ,       ,        ,J      ,8756    ,        ,        ,         ,        ,        ,
0916691DEL20151021             ,lkjhg.Random@random.com            ,lkjhg     ,Random        ,1565030,s051954,       ,        ,J      ,7654    ,        ,        ,         ,        ,        ,
0937846DEL110920151            ,lkjhgf.Random@random.com           ,lkjhgf    ,Random        ,       ,       ,       ,        ,J      ,6543    ,        ,        ,         ,        ,        ,
10262013%DEL%8912644DEL10672015,lkjhgfd.Random@random.com          ,lkjhgfd   ,Random        ,0064035,       ,       ,20150928,J      ,        ,        ,        ,         ,        ,        ,
10262013%DEL%8941949DEL106815  ,bnvxz.Random@random.com            ,bnvxz     ,Random        ,886442 ,       ,       ,20151030,J      ,        ,        ,        ,         ,        ,        ,
10262013%DEL%8943747DEL10692015,zxcv.Random@random.com             ,zxcv      ,Random        ,       ,       ,       ,20151030,J      ,        ,        ,        ,         ,        ,        ,
10262013%DEL%8946048DEL20651027,sgarnwxasf.Random@random.com       ,sgarnwxasf,Random        ,1173175,       ,       ,20150801,J      ,        ,        ,        ,SRandom  ,        ,        ,
10262013%DEL%8949542DEL106915  ,Bob.Bob@Normal.com                 ,Bob       ,Bob           ,       ,       ,       ,20150920,X      ,        ,        ,        ,BBob     ,        ,        ,
10262013%DEL%8953549DEL106115  ,                                   ,John      ,Doe           ,       ,       ,       ,20151131,J      ,        ,        ,        ,         ,        ,        ,
10262013%DEL%8957746DEL116315  ,Homer.Simpson@Simpsons.com         ,Homer     ,Simpson       ,T131456,       ,       ,20150928,X      ,        ,        ,        ,HSimpson ,        ,        ,
10262013%DEL%8961640           ,Peter.Griffon@familyguy.com        ,Peter     ,Griffon       ,1131998,       ,       ,20151030,J      ,        ,        ,        ,         ,        ,        ,
10262013%DEL%8961742           ,                                   ,John      ,Steward       ,1131823,       ,       ,20150414,J      ,        ,        ,        ,JSteward ,        ,        ,
10262013%DEL%8968648DEL10692015,                                   ,Hello     ,World         ,1131440,       ,       ,20170531,J      ,        ,        ,        ,         ,        ,        ,
10262013%DEL%8968649DEL10692015,                                   ,Hello     ,World         ,1131440,       ,       ,20170531,J      ,        ,        ,        ,         ,        ,        ,

现在我使用以下代码删除前导零...

Now I use the following code to remove the leading zeros...

Write-Host "Removing leading zeros from column `"column1`" in C:\path\test.csv"
Import-Csv "C:\path\test.csv" | 
    select -Property @{n='column1';e={[int]$_.column1}},* -Exclude 'column1' |
    Export-Csv "C:\path\testtemp" -NoTypeInformation
Move-Item "C:\path\testtemp" "C:\path\test.csv" -Force
Write-Host "Complete"

问题是字符串(带有 alpha 和特殊字符)无法转换为 [int],它们被删除,如下所示...

The issue is where the strings (with alpha and special characters) could not be converted into [int], they get removed as shown below...

"column1"  ,"column2"                     ,"column3"   ,"column4"       ,"column5","column6","column7","column8" ,"column9","column10","column11","column12","column13","column14","column15","column16"
"13"       ,""                            ,"ShopKeep"  ,"Store"         ,""       ,""       ,""       ,""        ,""       ,"1034627" ,""        ,""        ,""        ,""        ,""        ,""
"32"       ,""                            ,"Test411"   ,"User411"       ,"0007643",""       ,""       ,""        ,""       ,"1034632" ,""        ,""        ,""        ,""        ,""        ,""
"142"      ,""                            ,"Pinky"     ,"Brain"         ,"0571253",""       ,""       ,""        ,"J"      ,"1234"    ,""        ,""        ,""        ,""        ,""        ,""
"145"      ,"Leo@ninjaturtles.com"        ,"Leo"       ,"Turtle"        ,"0661203",""       ,""       ,""        ,"J"      ,"2345"    ,""        ,""        ,"RNixon"  ,""        ,""        ,""
"264"      ,""                            ,"Richard"   ,"Nixon"         ,"0311247",""       ,""       ,""        ,"J"      ,"3456"    ,""        ,""        ,""        ,""        ,""        ,""
"291"      ,"asdfasd.Random@random.com"   ,"asdfasd"   ,"Random"        ,"0841212",""       ,""       ,""        ,"J"      ,"4567"    ,""        ,""        ,"aRandom" ,""        ,""        ,""
"8984207"  ,"gfdgssg.Random@random.com"   ,"gfdgssg"   ,"Random"        ,"0883461","p209557",""       ,"20160531","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
"8984208"  ,"hgdhdfd.Random@random.com"   ,"hgdhdfd"   ,"Random"        ,"0883461","p209558",""       ,"20151202","J"      ,""        ,"P209558" ,""        ,""        ,""        ,""        ,""
"8984209"  ,"blankity.Random@random.com"  ,""          ,"Random"        ,"0883461","p209559",""       ,"20160531","J"      ,""        ,"P209559" ,""        ,""        ,""        ,""        ,""
"8984210"  ,"zxcvzvc.Random@random.com"   ,"zxcvzvc"   ,"Random"        ,"0883461","p209581",""       ,"20151202","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
"231716352","asdf-Random@random.com"      ,"asdf"      ,"Random"        ,""       ,""       ,""       ,""        ,""       ,"5678"    ,""        ,""        ,""        ,""        ,""        ,""
           ,""                            ,"asdfg"     ,"Random"        ,""       ,"s051699",""       ,""        ,"J"      ,"6789"    ,""        ,""        ,""        ,""        ,""        ,""
           ,"asdfgh.Random@random.com"    ,"asdfgh"    ,"Random"        ,"1565030","sB597RQ",""       ,""        ,"J"      ,"7890"    ,""        ,""        ,""        ,""        ,""        ,""
           ,"asdfghj.Random@random.com"   ,"asdfghj"   ,"Random"        ,"1562482","s052177",""       ,""        ,"J"      ,"0987"    ,""        ,""        ,""        ,""        ,""        ,""
           ,""                            ,"asdfghjs"  ,"Random"        ,"0561118","s051645",""       ,"20150322","J"      ,"9867"    ,""        ,""        ,""        ,""        ,""        ,""
           ,""                            ,"lkjh"      ,"Random"        ,""       ,"s85GRHZ",""       ,""        ,"J"      ,"8756"    ,""        ,""        ,""        ,""        ,""        ,""
           ,"lkjhg.Random@random.com"     ,"lkjhg"     ,"Random"        ,"1565030","s051954",""       ,""        ,"J"      ,"7654"    ,""        ,""        ,""        ,""        ,""        ,""
           ,"lkjhgf.Random@random.com"    ,"lkjhgf"    ,"Random"        ,""       ,""       ,""       ,""        ,"J"      ,"6543"    ,""        ,""        ,""        ,""        ,""        ,""
           ,"lkjhgfd.Random@random.com"   ,"lkjhgfd"   ,"Random"        ,"0064035",""       ,""       ,"20150928","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
           ,"bnvxz.Random@random.com"     ,"bnvxz"     ,"Random"        ,"886442" ,""       ,""       ,"20151030","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
           ,"zxcv.Random@random.com"      ,"zxcv"      ,"Random"        ,""       ,""       ,""       ,"20151030","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
           ,"sgarnwxasf.Random@random.com","sgarnwxasf","Random"        ,"1173175",""       ,""       ,"20150801","J"      ,""        ,""        ,""        ,"SRandom" ,""        ,""        ,""
           ,"Bob.Bob@Normal.com"          ,"Bob"       ,"Bob"           ,""       ,""       ,""       ,"20150920","X"      ,""        ,""        ,""        ,"BBob"    ,""        ,""        ,""
           ,""                            ,"John"      ,"Doe"           ,""       ,""       ,""       ,"20151131","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
           ,"Homer.Simpson@Simpsons.com"  ,"Homer"     ,"Simpson"       ,"T131456",""       ,""       ,"20150928","X"      ,""        ,""        ,""        ,"HSimpson",""        ,""        ,""
           ,"Peter.Griffon@familyguy.com" ,"Peter"     ,"Griffon"       ,"1131998",""       ,""       ,"20151030","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
           ,""                            ,"John"      ,"Steward"       ,"1131823",""       ,""       ,"20150414","J"      ,""        ,""        ,""        ,"JSteward",""        ,""        ,""
           ,""                            ,"Hello"     ,"World"         ,"1131440",""       ,""       ,"20170531","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""
           ,""                            ,"Hello"     ,"World"         ,"1131440",""       ,""       ,"20170531","J"      ,""        ,""        ,""        ,""        ,""        ,""        ,""

最终目标

我需要最终结果看起来像这样...

The End Goal

I need the end result to look like this...

"column1"                        ,"column2"                     ,"column3"   ,"column4"       ,"column5","column6","column7","column8" ,"column9","column10","column11","column12","column13","column14","column15","column16"
"13"                             ,""                            ,"ShopKeep"  ,"Store"         ,""       ,""       ,""       ,""        ,""       ,"1034627" ,""        ,""        ,""         ,""        ,""        ,""
"32"                             ,""                            ,"Test411"   ,"User411"       ,"0007643",""       ,""       ,""        ,""       ,"1034632" ,""        ,""        ,""         ,""        ,""        ,""
"142"                            ,""                            ,"Pinky"     ,"Brain"         ,"0571253",""       ,""       ,""        ,"J"      ,"1234"    ,""        ,""        ,""         ,""        ,""        ,""
"145"                            ,"Leo@ninjaturtles.com"        ,"Leo"       ,"Turtle"        ,"0661203",""       ,""       ,""        ,"J"      ,"2345"    ,""        ,""        ,"RNixon"   ,""        ,""        ,""
"264"                            ,""                            ,"Richard"   ,"Nixon"         ,"0311247",""       ,""       ,""        ,"J"      ,"3456"    ,""        ,""        ,""         ,""        ,""        ,""
"291"                            ,"asdfasd.Random@random.com"   ,"asdfasd"   ,"Random"        ,"0841212",""       ,""       ,""        ,"J"      ,"4567"    ,""        ,""        ,"aRandom"  ,""        ,""        ,""
"8984207"                        ,"gfdgssg.Random@random.com"   ,"gfdgssg"   ,"Random"        ,"0883461","p209557",""       ,"20160531","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"8984208"                        ,"hgdhdfd.Random@random.com"   ,"hgdhdfd"   ,"Random"        ,"0883461","p209558",""       ,"20151202","J"      ,""        ,"P209558" ,""        ,""         ,""        ,""        ,""
"8984209"                        ,"blankity.Random@random.com"  ,""          ,"Random"        ,"0883461","p209559",""       ,"20160531","J"      ,""        ,"P209559" ,""        ,""         ,""        ,""        ,""
"8984210"                        ,"zxcvzvc.Random@random.com"   ,"zxcvzvc"   ,"Random"        ,"0883461","p209581",""       ,"20151202","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"231716352"                      ,"asdf-Random@random.com"      ,"asdf"      ,"Random"        ,""       ,""       ,""       ,""        ,""       ,"5678"    ,""        ,""        ,""         ,""        ,""        ,""
"0118969DEL20151007"             ,""                            ,"asdfg"     ,"Random"        ,""       ,"s051699",""       ,""        ,"J"      ,"6789"    ,""        ,""        ,""         ,""        ,""        ,""
"0472549DEL20151021"             ,"asdfgh.Random@random.com"    ,"asdfgh"    ,"Random"        ,"1565030","sB597RQ",""       ,""        ,"J"      ,"7890"    ,""        ,""        ,""         ,""        ,""        ,""
"0812310DEL111115"               ,"asdfghj.Random@random.com"   ,"asdfghj"   ,"Random"        ,"1562482","s052177",""       ,""        ,"J"      ,"0987"    ,""        ,""        ,""         ,""        ,""        ,""
"0864549DEL20151029"             ,""                            ,"asdfghjs"  ,"Random"        ,"0561118","s051645",""       ,"20150322","J"      ,"9867"    ,""        ,""        ,""         ,""        ,""        ,""
"0895960DEL20151007"             ,""                            ,"lkjh"      ,"Random"        ,""       ,"s85GRHZ",""       ,""        ,"J"      ,"8756"    ,""        ,""        ,""         ,""        ,""        ,""
"0916691DEL20151021"             ,"lkjhg.Random@random.com"     ,"lkjhg"     ,"Random"        ,"1565030","s051954",""       ,""        ,"J"      ,"7654"    ,""        ,""        ,""         ,""        ,""        ,""
"0937846DEL110920151"            ,"lkjhgf.Random@random.com"    ,"lkjhgf"    ,"Random"        ,""       ,""       ,""       ,""        ,"J"      ,"6543"    ,""        ,""        ,""         ,""        ,""        ,""
"10262013%DEL%8912644DEL10672015","lkjhgfd.Random@random.com"   ,"lkjhgfd"   ,"Random"        ,"0064035",""       ,""       ,"20150928","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"10262013%DEL%8941949DEL106815"  ,"bnvxz.Random@random.com"     ,"bnvxz"     ,"Random"        ,"886442" ,""       ,""       ,"20151030","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"10262013%DEL%8943747DEL10692015","zxcv.Random@random.com"      ,"zxcv"      ,"Random"        ,""       ,""       ,""       ,"20151030","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"10262013%DEL%8946048DEL20651027","sgarnwxasf.Random@random.com","sgarnwxasf","Random"        ,"1173175",""       ,""       ,"20150801","J"      ,""        ,""        ,""        ,"SRandom"  ,""        ,""        ,""
"10262013%DEL%8949542DEL106915"  ,"Bob.Bob@Normal.com"          ,"Bob"       ,"Bob"           ,""       ,""       ,""       ,"20150920","X"      ,""        ,""        ,""        ,"BBob"     ,""        ,""        ,""
"10262013%DEL%8953549DEL106115"  ,""                            ,"John"      ,"Doe"           ,""       ,""       ,""       ,"20151131","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"10262013%DEL%8957746DEL116315"  ,"Homer.Simpson@Simpsons.com"  ,"Homer"     ,"Simpson"       ,"T131456",""       ,""       ,"20150928","X"      ,""        ,""        ,""        ,"HSimpson" ,""        ,""        ,""
"10262013%DEL%8961640"           ,"Peter.Griffon@familyguy.com" ,"Peter"     ,"Griffon"       ,"1131998",""       ,""       ,"20151030","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"10262013%DEL%8961742"           ,""                            ,"John"      ,"Steward"       ,"1131823",""       ,""       ,"20150414","J"      ,""        ,""        ,""        ,"JSteward" ,""        ,""        ,""
"10262013%DEL%8968648DEL10692015",""                            ,"Hello"     ,"World"         ,"1131440",""       ,""       ,"20170531","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""
"10262013%DEL%8968649DEL10692015",""                            ,"Hello"     ,"World"         ,"1131440",""       ,""       ,"20170531","J"      ,""        ,""        ,""        ,""         ,""        ,""        ,""

到目前为止我尝试过的...

我尝试将代码编辑为以下内容...

What I Have Tried So Far...

I have tried editing the code to the following...

Write-Host "Removing leading zeros from column `"column1`" in C:\path\test.csv"
Import-Csv "C:\path\test.csv" | 
    select -Property @{n='column1';e={If(([int]$_.column1 -ne $null) -or ([int]$_.column1 -ne "")){[int]$_.column1}Else{$_.column1}}},* -Exclude 'column1' |
    Export-Csv "C:\path\testtemp" -NoTypeInformation
Move-Item "C:\path\testtemp" "C:\path\test.csv" -Force
Write-Host "Complete"

但是不添加非int值的结果还是一样.如果有人可以帮助我解决语法问题,我将不胜感激.

But the result of not adding the non-int values is still the same. If anyone could help me with the syntax I would be grateful.

推荐答案

尝试使用.TrimStart()"去掉零.

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

请注意,为了使其正常工作,我必须重新格式化您的第一列以去除多余的空格.因此,原始 csv 中的标题将以:列 1、列 2 等.

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.

这篇关于仅从 int 值中删除 csv 文件中的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆