[英]how to convert from Newline Delimited text file to csv with Powershell
[英]How to create newline on csv file from powershell
我被困在一个点,我有两个像这样的数组变量
$name = @("A","B","C")
$roll = @("1","2","3")
我正在尝试用这些数据制作一个csv文件。 我正在使用此代码
$name = @("A","B","C")
$roll = @("1","2","3")
$report = New-Object psobject
$report | Add-Member -MemberType NoteProperty -Name "Name" -Value "$name"
$report | Add-Member -MemberType NoteProperty -Name "Roll" -Value "$roll"
$report | Export-Csv -Path "C:\Users\Abby\Desktop\r.csv" -NoTypeInformation
但我有这样的输出
NAME | 滚
ABC | 1 2 3
我希望我的输出像这样
NAME | 滚
A | 1
B | 2
C | 3
任何人都可以知道如何做这些...请帮助我...... :(
您当前的代码正在创建对象,并将数组$name
和$roll
为单个条目 。
听起来你想要一个数组中每个值的条目。 假设数组的长度相同,您可以迭代这些值:
$name = @("A","B","C")
$roll = @("1","2","3")
foreach($i in 0..($name.Count -1)){
[array]$report += [pscustomobject] @{
Name = $name[$i]
Roll = $roll[$i]
}
}
# screen output
# $report | Format-Table
$report | Export-Csv -Path "C:\Users\Abby\Desktop\r.csv" -NoTypeInformation
屏幕输出
Name Roll
---- ----
A 1
B 2
C 3
尝试这个:
$name = "A","B","C"
$roll = "1","2","3"
$index = 0
$name | %{[pscustomobject]@{Name=$_;Roll=$roll[$index++]}} |Export-csv "C:\Users\Abby\Desktop\r.csv" -NoType
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.