我正在尝试使用Powershell通过以下代码将rest API导出到CSV
$DesiredProperties | Export-CSV -Path ".\Testrestapi.csv" -Encoding ASCII -NoTypeInformation
它将以下内容输出到CSV:
, "105283","TickerID" "2365512290","TickerID" , , , , "US594918AH79","ISIN" , , , , "US594918AH79","ISIN" "2121687888","TickerID" "US594918AH79","ISIN" ,
所需结果:
"105283","TickerID" "2365512290","TickerID" "US594918AH79","ISIN" "US594918AH79","ISIN" "2121687888","TickerID" "US594918AH79","ISIN"
我尝试使用
(gc file.txt) | ? {$_.Trim() -ne ""} | Set-Content file.txt
,但似乎不适用于CSV文件。有人可以告诉我该脚本中应使用哪个命令删除空行“,,,”
答案 0 :(得分:1)
您可以使用Where-Object { $_.PSObject.Properties.Value -ne '' }
直接清理$DesiredProperties
对象。
示例:
输入数据:“ C:\ Temp \ Testrestapi.csv”(我在这里使用CSV,但是您应该直接从REST API中获取导出对象:
,
"105283","TickerID"
"2365512290","TickerID"
,
,
,
,
"US594918AH79","ISIN"
,
,
,
,
"US594918AH79","ISIN"
"2121687888","TickerID"
"US594918AH79","ISIN"
,
示例代码:
Import-Csv -Path "C:\Temp\Testrestapi.csv" -Header 'col1','col2' | Where-Object { $_.PSObject.Properties.Value -ne '' } | Export-Csv -Path "C:\Temp\Testrestapi_clean.csv" -NoTypeInformation
输出数据:“ C:\ Temp \ Testrestapi_clean.csv”:
"col1","col2"
"105283","TickerID"
"2365512290","TickerID"
"US594918AH79","ISIN"
"US594918AH79","ISIN"
"2121687888","TickerID"
"US594918AH79","ISIN"