简体   繁体   English

如何格式化使用Powershell import-csv获取的数据

[英]how to format data acquired using powershell import-csv

I imported a csv file using import-csv. 我使用import-csv导入了一个csv文件。 I only wanted the account number from the list. 我只想要列表中的帐号。 来自import-csv的数据

I used 我用了

$Numbers = Import-Csv csv.csv |Select-Object -ExpandProperty 'Account Number' |Where-Object {$_ -ne "0"} | Out-Null 

but i want enclose the account number with "'" and separate the account number with ",". 但我想用“'”括住帐号,并用“,”分开帐号。 Ideally the list should look like: 'account1','account2',...,'accountlast'. 理想情况下,列表应如下所示:'account1','account2',...,'accountlast'。 I could not manipulate the $ number variable like array. 我无法操纵像数组一样的$ number变量。

You are very close. 你很亲密 You can import the csv, select the object, then use a foreach to format each object, and finally write to the host with one line instead of breaks for each. 您可以导入csv,选择对象,然后使用foreach设置每个对象的格式,最后只用一行写到主机,而不是用每行中断。

Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"} | ForEach{"'" + $_ + "', "} | Write-Host -NoNewLine

Some string manipulation and a -join should be able to get this for you. 一些字符串操作和-join应该可以为您提供帮助。 Although an array which is returned from Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"} 尽管从Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"}返回的数组Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"} Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"} Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"} would be considered more versatile! Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"}被认为更加通用!

$numbers = "'{0}'" -f ((Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"}) -join "','")

Join the account numbers with ',' and then we use the format operator to enclose that into the outer single quotes. ','将帐号连接起来','然后使用格式运算符将其括在外部单引号中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM