无法在现有的CSV文件中添加额外的列 [英] Unable to add an extra column to an existing CSV file

查看:160
本文介绍了无法在现有的CSV文件中添加额外的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,其中包含以下数据:

I have a .csv file which contains data as below:


Name ID
ABC 1234
CDE 3456

我正在尝试将名为日期"的列作为第一列.

I am trying to insert a column called "Date" as the first column.


Date        Name     ID
2018-03-28  ABC     1234
2018-03-28  CDE     3456

我正在使用以下进出口:

I am using below import-export:

$a = Import-Csv -Path c:\filename.csv -Header Date,Name,Id
$a | Export-Csv -Path c:\outfile.csv -NoTypeInformation

但上述方法无法按预期工作.它正在替换列数据.

But above not working as expected. It's displacing the column data.


Date  Name  Id
ABC   1234
CDE   3456   

推荐答案

您需要将属性添加到对象:

You need to add the property to the objects:

$a= import-csv -path c:\filename.csv -header Name,Id
foreach($item in $a){
    Add-Member -Input $item -MemberType NoteProperty -Name Date -Value '2018-03-28'
}
$a | Select-Object Date, Name, ID | export-csv -path c:\outfile.csv -Notypeinformation

这篇关于无法在现有的CSV文件中添加额外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆