gpt4 book ai didi

powershell - 有没有更好的方法使用 Powershell 将标题添加到 CSV 文件?

转载 作者:行者123 更新时间:2023-12-04 00:10:21 24 4
gpt4 key购买 nike

有没有比硬编码更有效的方法将标题添加到 CSV 文件?

$Importfile = Import-csv $path$filename -header H1, H2, H3, H4, H5, H6, H7 #添加标题到csv文件

最佳答案

这是一种为没有标题行的 CSV 文件自动生成标题行的方法。它计算文件第一行中的项目以生成标题行。 [咧嘴一笑]

$FileName = "$env:TEMP\Alex_-_Headerless.csv"
$Delimiter = ','

#region >>> create a headerless CSV file to work with
# remove this section when you are ready to do stuff for real
$HeaderlessCSV = @'
1001, Jon, Smith, Anvil Inspector
2002, Bob, Archer, Bow Builder
3003, Mike, Carter, Wagon Designer
'@ | Set-Content -LiteralPath $FileName
#endregion >>> create a headerless CSV file to work with

$ColumnCount = @((Get-Content -LiteralPath $FileName -TotalCount 1).Split($Delimiter)).Count
$HeaderLine = foreach ($Index in 1..$ColumnCount)
{
'Col_{0}' -f $Index
}

$InStuff = Import-Csv -Delimiter $Delimiter -Header $HeaderLine -LiteralPath $FileName

$InStuff

输出...

Col_1 Col_2 Col_3  Col_4          
----- ----- ----- -----
1001 Jon Smith Anvil Inspector
2002 Bob Archer Bow Builder
3003 Mike Carter Wagon Designer

关于powershell - 有没有更好的方法使用 Powershell 将标题添加到 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57503888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com