2017-03-02 24 views
0

我使用Powershell和Selenium進行解析。Powershell。如何在Excel中導出結果數組?

用命令$fullstory = $ie.FindElementByClassName("comment")

我得到的輸出:

 
Artist: Pink Floyd 
Album Title: Dark Side Of the Moon 
Genre: Rock 
Year: 2016 

我想將它導出到Excel中,與「藝術家」,「唱片標題」等,作爲列標題和數據冒號後的列作爲值?

+0

目前尚不清楚您希望Excel中的數據,您是否希望藝術家,專輯標題,流派和年份作爲列標題以及列下的數據?或者在與你目前使用的佈局相同的情況下,使用':'作爲單元格分隔符? –

+0

Hello James C.對不起!我想獲得像「藝術家,專輯標題,流派和年份」欄目標題和他們下面的數據在列「 –

+0

你能發表[最小,完整和可驗證的例子](http://stackoverflow.com/幫助/ mcve),您的單行代碼不足以提供幫助,因爲我不知道輸出的數據類型。 –

回答

0

您可以創建一個對象,然後使用Export-CSV將其導出到.csv文件。

下面是一個假設$fullstory[string]而不是字符串數組的示例。如果它是一串字符串,請刪除$nl行和.split($nl)

$fullstory = $ie.FindElementByClassName("comment") 
$nl = [Environment]::NewLine 
$output = [pscustomobject]@{} 
foreach ($line in $fullstory.split($nl)) { 
    $split = $line.split(":") 
    $PropertyName = $split[0].Trim() 
    $PropertyValue = $split[1].Trim() 
    Add-Member -InputObject $output -Name $PropertyName -Value $PropertyValue -MemberType NoteProperty 
} 
$output | Export-CSV C:\example\example.csv -notypeinformation