【问题标题】:Export hash table to multiple columns in CSV in PowerShell在 PowerShell 中将哈希表导出到 CSV 中的多个列
【发布时间】:2020-10-01 12:25:33
【问题描述】:

我有大量文件,我想对其中的单词进行分析 - 计算每个单词在每个文件中出现的频率。作为最终输出,我想要一个 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

主要有两个问题:

  1. 每个文件的唯一字数不同(哈希表长度不同)
  2. 文件被逐一读取。因此,每个文件的结果需要在导出之前进行缓存。

不知何故,我没有得到我想要的输出,而只有元数据。如何获得正确的输出?

【问题讨论】:

  • 那么如果你有 3 个文件,每个文件有 30 个唯一的单词,你希望最终得到 180 列吗?
  • 使用Export-Csv -NoTypeInformation 阻止它显示元数据
  • @Doug:不。在这种情况下,我想要 6 列(3 次“单词”和“计数”),30 行。
  • @Theo -NoTypeInformation 只删除带有 TypeInformation 的第一行,而不是元数据输出。
  • 然后向我们展示您当前的输出并更好地解释所需的输出应该是什么

标签: powershell export-to-csv


【解决方案1】:

我花时间写了一个模拟你的情况。

# 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

【讨论】:

    猜你喜欢
    • 2018-01-22
    • 2018-09-16
    • 2021-05-29
    • 2020-05-13
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2011-07-01
    相关资源
    最近更新 更多