【发布时间】:2018-11-29 22:27:51
【问题描述】:
我正在处理两个 CSV 文件。一个保存用户的姓名,另一个保存他们相应的电子邮件地址。我想要做的是将它们结合起来,使用户是第 1 列,电子邮件是第 2 列,并将其输出到一个文件。到目前为止,我已经设法将电子邮件 csv 文件中的第二列添加到用户 csv 文件中,但使用空白行数据。以下是我正在使用的代码:
$emailCol= import-csv "C:\files\temp\emailOnly.csv" | Select-Object -skip 1
$emailArr=@{}
$i=0
$nameCol = import-csv "C:\files\temp\nameOnly.csv"
foreach ($item in $emailCol){
$nameCol | Select *, @{
Name="email";Expression=
{$emailArr[$i]}
} | Export-Csv -path
C:\files\temp\revised.csv -NoTypeInformation
}
更新:以下对我有用。谢谢 BenH!
function combineData {
#This function will combine the user CSV file and
#email CSV file into a single file
$emailCol = Get-Content "C:\files\temp\emailOnly.csv"
| Select-Object -skip 1
$nameCol = Get-Content "C:\files\temp\nameOnly.csv" |
Select-Object -skip 1
# Max function to find the larger count of the two
#csvs to use as the boundary for the counter.
$count = [math]::Max($emailCol.count,$nameCol.count)
$CombinedArray = for ($i = 0; $i -lt $count; $i++) {
[PSCustomObject]@{
fullName = $nameCol[$i]
email = $emailCol[$i]
}
}
$CombinedArray | Export-Csv C:\files\temp\revised.csv
-NoTypeInformation
}
【问题讨论】:
标签: arrays powershell csv multiple-columns