[英]Export hash table to multiple columns in CSV in PowerShell
我有大量文件,我想對其中的文件進行單詞分析 - 計算每個單詞在每個文件中出現的頻率。 作為最終輸出,我想要一個 CSV 文件,標題中包含文件名,每個文件有兩列 - 單詞和相應的計數。
file1 word, file1 count, file2 word, file2 count, ....
hello, 4, world, 5, ...
password, 10, save, 2, ...
為此,我打開每個文件並將字數保存在哈希表中。 因為每個哈希表都有不同的長度(不同數量的唯一詞),所以我嘗試將結果放在數據表中以導出它們。
$file = Get-ChildItem -Recurse
$out = New-Object System.Data.DataSet "ResultsSet"
foreach($f in $file){
$pres = $ppt.Presentations.Open($f.FullName, $true, $true, $false)
$id = $f.Name.substring(0,5)
$results = @{} #Hash table for this file
for($i = 4; $i -le $pres.Slides.Count; $i++){
$s = $pres.Slides($i)
$shapes = $s.Shapes
$textBox = $shapes | ?{$_.TextFrame.TextRange.Length -gt 100}
if($textBox -ne $null){
$textBox.TextFrame.TextRange.Words() | %{$_.Text.Trim()} | %{if(-not $results.ContainsKey("$_")){$results.Add($_,1)}else{$results["$_"] += 1 }}
}
}
$pres.Close()
$dt = New-Object System.Data.DataTable
$dt.TableName = $id
[String]$dt.Columns.Add("$id Word")
[Int]$dt.Columns.Add("$id Count")
foreach($r in ($results.GetEnumerator() | sort Value)) {
$dt.Rows.Add($r.Key, $r.Value)
}
$out.Tables.Add($dt)
}
$out | export-csv
主要有兩個問題:
不知何故,我沒有得到我想要的輸出,而只有元數據。 我怎樣才能獲得正確的輸出?
我花時間寫了一個模擬你的情況。
# File names. The number of files should match the number of hash tables
$Files = 'file1','file2','file3','file4','file5'
# hash table results per file (simulated)
$HashPerFile = [ordered]@{ hello = 4; goodbye = 3; what = 1; is = 7; this = 4 },
[ordered]@{ password = 2; hope = 1; they = 3; are = 2; not = 5; plain = 2; text = 18},
[ordered]@{ help = 6; me = 2; please = 5 },
[ordered]@{ decrypt = 1; the = 3; problem = 1 },
[ordered]@{ because = 2; I = 5; cannot = 9 }
# Headers for the object output
$properties = $Files |% {"$_ word";"$_ count"}
# Determining max number of rows in results based on highest hash table length
$MaxRows = [linq.enumerable]::max([int[]]($hashperfile |% {$_.Count}))
# Precreating the result array $r
$r = 1..$MaxRows |% { "" | select $properties }
# Index of $properties. This helps select the correct 'file word' and 'file count' property
$pIndex = 0
# for loop to go through each file and hash table
for ($i = 0; $i -lt $files.count; $i++) {
# rIndex is the index of the $r array.
# When a new file is selected, this needs to reset to 0 so we can begin at the top of the $r array again.
$rIndex = 0
# Iterate the hash table that matches the file. Index $i ensures this.
$hashPerFile[$i].GetEnumerator() |% {
$r[$rIndex].$($properties[$pIndex]) = $_.Key
$r[$rIndex++].$($properties[$pIndex+1]) = $_.Value
}
# Have to use +2 because there are two properties for each file
$pIndex += 2
}
$r # Output
$r | Export-Csv output.csv -NoType # CSV output
我有大量文件,我想對它們進行詞分析-計算每個文件中每個詞出現的頻率。 作為最終輸出,我希望有一個CSV文件,標題中帶有文件名,每個文件都有兩列-word和相應的計數。
file1 word, file1 count, file2 word, file2 count, ....
hello, 4, world, 5, ...
password, 10, save, 2, ...
為此,我打開每個文件並將字數保存在哈希表中。 因為每個哈希表的長度都不同(唯一字的數量不同),所以我嘗試將結果放入數據表中以將其導出。
$file = Get-ChildItem -Recurse
$out = New-Object System.Data.DataSet "ResultsSet"
foreach($f in $file){
$pres = $ppt.Presentations.Open($f.FullName, $true, $true, $false)
$id = $f.Name.substring(0,5)
$results = @{} #Hash table for this file
for($i = 4; $i -le $pres.Slides.Count; $i++){
$s = $pres.Slides($i)
$shapes = $s.Shapes
$textBox = $shapes | ?{$_.TextFrame.TextRange.Length -gt 100}
if($textBox -ne $null){
$textBox.TextFrame.TextRange.Words() | %{$_.Text.Trim()} | %{if(-not $results.ContainsKey("$_")){$results.Add($_,1)}else{$results["$_"] += 1 }}
}
}
$pres.Close()
$dt = New-Object System.Data.DataTable
$dt.TableName = $id
[String]$dt.Columns.Add("$id Word")
[Int]$dt.Columns.Add("$id Count")
foreach($r in ($results.GetEnumerator() | sort Value)) {
$dt.Rows.Add($r.Key, $r.Value)
}
$out.Tables.Add($dt)
}
$out | export-csv
有兩個主要問題:
不知何故,我沒有得到想要的輸出,而只有元數據。 如何獲得正確的輸出?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.