我有3个csv文件,它们只有1列长。我已经尝试了很多东西把它全部放在一个csv文件中,但我无法让它工作。当我输出它时,这一切最终都在一列中,这是我到目前为止所做的
#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
答案 0 :(得分:0)
你的循环正在将所有内容添加到$ CSV4中,每次循环$ CSV4越来越长。
然后打印一次。这就是为什么你得到一个很长的路线。每次循环尝试打印一次,每次都覆盖$ CSV4:
#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
}
答案 1 :(得分:0)
使用格式字符串。
$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
}
答案 2 :(得分:0)
作为一个更有趣的答案:
$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++}
示例输出:
1, 10, a
2, 11, b
3, 12, c
4, 13, d
您可以将|Out-File "merged-data.csv"
放在最后以保存到文件中。
它也适用于更多列,只需添加更多名为$CSV{something}
的数组。
编辑:我想知道Get-Variable的输出是否是可预测的顺序,还是未指定?如果您不介意列顺序可能会更改,则会折叠为:
$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++}
再次编辑:好吧,如果有人注意到并且好奇,并且有时间在他们手上,我已经将它全部扩展到解释它正在做什么:
# 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++
}