【问题标题】:How do I add headers into a CSV that does not have one?如何将标题添加到没有标题的 CSV 中?
【发布时间】:2015-02-16 17:21:34
【问题描述】:

听起来很简单,但我做不到......

$file = 'D:\TESTING.csv'

Set-Content $file "1,2,3"

$file = import-csv $file -Header a , b , c | export-csv $file 

echo $file

期望的输出:

a b c
- - -
1 2 3

实际输出:

nothing

【问题讨论】:

标签: powershell csv


【解决方案1】:

就是这一行:

$file = import-csv $file -Header a , b , c | export-csv $file

您正在将数据传送到export-csv cmdlet。该命令没有输出,因此$file 将为空。 $file 还包含您的输出路径。为什么要将其更改为文件内容?

假设您既要导出数据又要将其保留在会话中,您可以改为执行以下操作:

$filedata = import-csv $file -Header a , b , c  
$filedata | export-csv $file -NoTypeInformation

你也可以用Tee-Object一行来完成

Import-CSV $file -Header a , b , c | Tee-Object -Variable $filedata | Export-CSV $file -NoTypeInformation

【讨论】:

    【解决方案2】:

    这应该可行:

    import-csv test.csv -Header "Name" | export-csv test.csv -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多