首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用export-csv时遇到问题

使用export-csv时遇到问题
EN

Stack Overflow用户
提问于 2014-03-29 05:53:39
回答 3查看 98关注 0票数 0

我有3个csv文件,它们都只有1列长。我已经尝试了很多东西来把它全部放在一个csv文件中,但我就是不能让它工作。当我输出它时,所有的结果都在一列中,这是我到目前为止所做的

代码语言:javascript
复制
#Putting Csv files to Array    
$CSV1 = @(gc $EmailPathCsv)
$CSV2 = @(gc $UserLPathCsv)
$CSV3 = @(gc $EmailPathCsv)

#
for ($i=0; $i -lt $CSV1.Count; $i++) 
    {
    $CSV4 += $CSV1[$i] + "," + $CSV2[$i] + "," + $CSV3[$i] + " "
    }

$csv4 | out-file -append $MergedCsvExport
EN

回答 3

Stack Overflow用户

发布于 2014-03-29 06:05:47

您的循环正在将所有内容添加到$CSV4中,每次通过循环$CSV4都会变得越来越长。

然后再打印一次。这就是为什么你会得到一条很长的线。尝试在循环中每次打印一次,并每次覆盖$CSV4:

代码语言:javascript
复制
#Putting Csv files to Array    
$CSV1 = @(gc $EmailPathCsv)
$CSV2 = @(gc $UserLPathCsv)
$CSV3 = @(gc $EmailPathCsv)

#
for ($i=0; $i -lt $CSV1.Count; $i++) 
    {
    $CSV4 = $CSV1[$i] + "," + $CSV2[$i] + "," + $CSV3[$i] + " "
    out-file -InputObject $csv4 -Append -FilePath $MergedCsvExport
    }
票数 0
EN

Stack Overflow用户

发布于 2014-03-29 06:33:20

为此,我会使用foromat字符串。

代码语言:javascript
复制
$CSV1 = @(gc $EmailPathCsv)
$CSV2 = @(gc $UserLPathCsv)
$CSV3 = @(gc $EmailPathCsv)

for ($i=0; $i -lt $CSV1.Count; $i++) 
 {
  '"{0}","{1}","{2}"' -f $CSV1[$i],$CSV2[$i],$CSV3[$i] |
   add-content $MergedCsvExport
 }
票数 0
EN

Stack Overflow用户

发布于 2014-03-29 07:45:52

作为一个更有趣的答案:

代码语言:javascript
复制
$CSV1 = 1,2,3,4
$CSV2 = 10,11,12,13
$CSV3 = 'a','b','c','d'

$c = gv -Name CSV* | select -expand name | sort

(gv -Va $c[0])|%{$r=0}{@($c|%{(gv -Va $_)[$r]}) -join ',';$r++}

示例输出:

代码语言:javascript
复制
1, 10, a
2, 11, b
3, 12, c
4, 13, d

您可以将|Out-File "merged-data.csv"放在末尾以保存到文件中。

它也适用于更多的列,只需添加更多名为$CSV{something}的数组。

编辑:我想知道Get-Variable的输出是以可预测的顺序,还是未指定的顺序?如果您不介意列顺序可能会更改,它将折叠为:

代码语言:javascript
复制
$CSV1 = 1,2,3,4
$CSV2 = 10,11,12,13
$CSV3 = 'a','b','c','d'

(gv CSV*)[0].Value|%{$r=0}{@((gv CSV*)|%{(gv -Va $_.Name)[$r]}) -join ',';$r++}

再次编辑:好吧,如果有人注意到了,很好奇,并且有时间,我已经扩展了它,解释它是做什么的:

代码语言:javascript
复制
# Search the local variable scope for variables named CSV*
# This will find $CSV1, $CSV2, etc.
# This means the number of columns
# isn't fixed, you can easily add more.
# Take their names, sort them. 
# Result: an array of strings "CSV1", "CSV2", ...
# for however many variables there are
$columnVariables = Get-Variable -Name "CSV*" | Select-Object -Expand Name | Sort-Object

# NB. if you remove $CSV3= from your code, it is
# still in memory from previous run. To remove
# it, use `Remove-Variable "CSV3"

# In pseudo-code, what the next part does is
# for row in range(data):
#    @(CSV1[row], CSV2[row], ... CSVn[row]) -join ','

# The outer loop over the number of columns
# is done by piping something of the right length
# into a foreach loop, but ignoring the content.
# So get the first column array content:
$firstColumn = (Get-Variable $columnVariables[0]).Value

# and pipe it into a loop.
# The loop uses ForEach {setup} {loop} pattern 
# to setup a row-counter before the loop body
$firstColumn | ForEach { $row=0 } {

    # Now we know the row we are on, we can get
    # $CSV1[row], $CSV2[row], ...
    # Take the column variable array "CSV1", "CSV2", ..
    # Loop over them
    $rowContent = $columnVariables | ForEach {

        # $_ a string of the name, e.g. "CSV1"
        # Use Get-Variable to convert it
        # into the variable $CSV1
        # with -ValueOnly to get the array itself
        # rather than details about the variable
        $columnVar = Get-Variable -ValueOnly $_

        # $columVar is now one of the $CSVn variables
        # so it contains, e.g. 1,2,3,4
        # Index into that for the current row
        # to get one item, e.g. 3
        # Output it into the pipeline
        ($columnVar)[$row]

        } # This is the end of the inner loop
          # The pipeline now contains the column
          # values/content making up a single row
          # 1
          # 10
          # 'a'

    # Back in the outer row loop, Take the row
    # content and make it a comma separated string
    # e.g. "1,10,a"
    # Output this into the pipeline
    @($rowContent) -join ','

    # Increment the row counter
    $row++
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22723829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档