CSV/Powershell 添加一个 -Header
CSV/Powershell Add an -Header
我有这个循环:
foreach($line in Get-Content .\script2.csv)
{ $firstname = $line.split(';')[0]
$lastname = $line.split(';')[1]
$email = $line.split(';')[2]
$newLine = """$firstname"",""$lastname"",""$email"""
$newLine >> newCSV.csv }
我想添加一个特定的 header 但我不知道应该在我的小脚本中的什么地方写“-header”。
谢谢
编辑:
我有一个这样的文件:
A;B;C
A;B;C
A;B;C
A;B;C
我需要这个:
"info1","info2","info3",...,"info18"
"A","B",C","","","","","","","","","","","","","","",""
"A","B",C","","","","","","","","","","","","","","",""
"A","B",C","","","","","","","","","","","","","","",""
"A","B",C","","","","","","","","","","","","","","",""
正如我所评论的,这可以简单地在没有循环的情况下完成:
您输入的 CSV (script2.csv)
A;B;C
A;B;C
A;B;C
A;B;C
# get objects from the input csv file and provide headers for the data
# then save this data with the default delimiter comma to a new file
Import-Csv -Path '.\script2.csv' -Delimiter ';' -Header 'info1','info2','info3','info4','info5' |
Export-Csv -Path '.\newCsv.csv' -NoTypeInformation
会给你一个新文件 'newCsv.csv' 看起来像这样:
"info1","info2","info3","info4","info5"
"A","B","C",,
"A","B","C",,
"A","B","C",,
"A","B","C",,
如果您需要更多字段,就像您评论的那样,您可以简单地创建一个字符串数组来存放所有想要的 headers 并使用它。
类似
$header = 1..18 | ForEach-Object {"info$_"}
Import-Csv -Path '.\script2.csv' -Delimiter ';' -Header $header |
Export-Csv -Path '.\newCsv.csv' -NoTypeInformation
这将使用以下内容保存 newCsv.csv:
"info1","info2","info3","info4","info5","info6","info7","info8","info9","info10","info11","info12","info13","info14","info15","info16","info17","info18"
"A","B","C",,,,,,,,,,,,,,,
"A","B","C",,,,,,,,,,,,,,,
"A","B","C",,,,,,,,,,,,,,,
"A","B","C",,,,,,,,,,,,,,,
任何读取此类文件的软件都应该能够处理空字段,因为绝对没有理由用""
填充这些字段以表示该字段是空.. Reading/Importing CSV 应该始终 return 值作为 strings 无论如何,正如 Import-Csv 那样。
- Import-Csv 从 delimiter-separated 值文件中的项目创建 table-like 自定义 objects。通常这个分隔符是逗号,因此扩展名 Comma Separated Values (.csv)
- Export-Csv 将 objects 转换为一系列 delimiter-separated 值 (CSV) 字符串并将字符串保存到文件中。
- Get-Content 获取文本文件的内容和 returns 一个字符串数组,其中的行按换行符拆分。
向此 cmdlet 添加开关 -Raw
时,它 return 将文件内容作为包含换行符的单个多行字符串。
我有这个循环:
foreach($line in Get-Content .\script2.csv)
{ $firstname = $line.split(';')[0]
$lastname = $line.split(';')[1]
$email = $line.split(';')[2]
$newLine = """$firstname"",""$lastname"",""$email"""
$newLine >> newCSV.csv }
我想添加一个特定的 header 但我不知道应该在我的小脚本中的什么地方写“-header”。
谢谢
编辑:
我有一个这样的文件:
A;B;C
A;B;C
A;B;C
A;B;C
我需要这个:
"info1","info2","info3",...,"info18"
"A","B",C","","","","","","","","","","","","","","",""
"A","B",C","","","","","","","","","","","","","","",""
"A","B",C","","","","","","","","","","","","","","",""
"A","B",C","","","","","","","","","","","","","","",""
正如我所评论的,这可以简单地在没有循环的情况下完成:
您输入的 CSV (script2.csv)
A;B;C
A;B;C
A;B;C
A;B;C
# get objects from the input csv file and provide headers for the data
# then save this data with the default delimiter comma to a new file
Import-Csv -Path '.\script2.csv' -Delimiter ';' -Header 'info1','info2','info3','info4','info5' |
Export-Csv -Path '.\newCsv.csv' -NoTypeInformation
会给你一个新文件 'newCsv.csv' 看起来像这样:
"info1","info2","info3","info4","info5"
"A","B","C",,
"A","B","C",,
"A","B","C",,
"A","B","C",,
如果您需要更多字段,就像您评论的那样,您可以简单地创建一个字符串数组来存放所有想要的 headers 并使用它。
类似
$header = 1..18 | ForEach-Object {"info$_"}
Import-Csv -Path '.\script2.csv' -Delimiter ';' -Header $header |
Export-Csv -Path '.\newCsv.csv' -NoTypeInformation
这将使用以下内容保存 newCsv.csv:
"info1","info2","info3","info4","info5","info6","info7","info8","info9","info10","info11","info12","info13","info14","info15","info16","info17","info18"
"A","B","C",,,,,,,,,,,,,,,
"A","B","C",,,,,,,,,,,,,,,
"A","B","C",,,,,,,,,,,,,,,
"A","B","C",,,,,,,,,,,,,,,
任何读取此类文件的软件都应该能够处理空字段,因为绝对没有理由用""
填充这些字段以表示该字段是空.. Reading/Importing CSV 应该始终 return 值作为 strings 无论如何,正如 Import-Csv 那样。
- Import-Csv 从 delimiter-separated 值文件中的项目创建 table-like 自定义 objects。通常这个分隔符是逗号,因此扩展名 Comma Separated Values (.csv)
- Export-Csv 将 objects 转换为一系列 delimiter-separated 值 (CSV) 字符串并将字符串保存到文件中。
- Get-Content 获取文本文件的内容和 returns 一个字符串数组,其中的行按换行符拆分。
向此 cmdlet 添加开关-Raw
时,它 return 将文件内容作为包含换行符的单个多行字符串。