Import-Csv-成员已经存在的问题 [英] Import-Csv - Member already present issue

查看:298
本文介绍了Import-Csv-成员已经存在的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将多个CSV文件合并为一个文件.每个CSV都有一个标头.列标题之一是相同的.理想情况下,最终文件(all_out.csv)必须具有单个标头.

I have to combine multiple CSV files into a single one. Each of the CSV has a header. One of the columns header is identical. Ideally, the end file (all_out.csv) has to have a single header.

我运行PowerShell代码:

I run the PowerShell code:

Import-Csv out_1_result.csv,out_2_result.csv,out_3_result.csv,out_4_result.csv,out_5_result.csv,out_6_result.csv,out_7_result.csv,out_8_result.csv,out_9_result.csv,out_10_result.csv,out_11_result.csv,out_12_result.csv,out_13_result.csv,out_14_result.csv,out_15_result.csv,out_16_result.csv,out_17_result.csv,out_18_result.csv,out_19_result.csv,out_20_result.csv,out_21_result.csv,out_22_result.csv,out_23_result.csv,out_24_result.csv,out_25_result.csv,out_26_result.csv,out_27_result.csv,out_28_result.csv |
    Export-Csv all_out.csv -NoType 

最后我遇到了错误

Import-Csv:成员"URL"已经存在.

Import-Csv : The member "URL" is already present.

有没有办法忽略/解决这个问题?

Is there a way to ignore/fix this?

推荐答案

其中一个列标题相同

One of the columns header is identical

这意味着每个CSV都有两列标题"URL"吗? Import-Csv创建对象,其中每个标头都变为属性名称,例如@{Id=10; Url='example.com'}并再次使用相同的名称将发生冲突.

That means each CSV has two columns header 'URL'? Import-Csv creates objects where each header becomes a property name, e.g. @{Id=10; Url='example.com'} and using the same name again will clash.

如果不更改csv文件,这将无法正常工作,因为仅使用Import-Csv cmdlet就无法说使用不同的列名"和跳过标题行".

There is no way this will work cleanly without you changing the csv files, as there is no way to say "use different column names" and also "skip the header row" just with the Import-Csv cmdlet.

我能想到的最简单的更改是从每个标题行中删除标题行,例如:

The easiest change I can think of is to drop the header line from each one, e.g.:

$CsvFiles = 'out_1_result.csv','out_2_result.csv','out_3_result.csv','out_4_result.csv','out_5_result.csv','out_6_result.csv','out_7_result.csv','out_8_result.csv','out_9_result.csv','out_10_result.csv','out_11_result.csv','out_12_result.csv','out_13_result.csv','out_14_result.csv','out_15_result.csv','out_16_result.csv','out_17_result.csv','out_18_result.csv','out_19_result.csv','out_20_result.csv','out_21_result.csv','out_22_result.csv','out_23_result.csv','out_24_result.csv','out_25_result.csv','out_26_result.csv','out_27_result.csv','out_28_result.csv'

$NewFileNames = $CsvFiles | ForEach-Object { 

    $NewFileName = $_ + "_noheader.csv"

    Get-Content $_ | Select-Object -Skip 1 | Set-Content $NewFileName -Encoding UTF8

    $NewFileName   # write new name to output stream

}

然后,当它们没有标题行时,将它们全部导入并指定标题行作为参数

And then, when they have no header line, import them all and specify the header line as a parameter

Import-Csv -Path $NewFileNames -Header 'Col1', 'Col2', 'Url1', 'Url2' | Export-Csv ...

这篇关于Import-Csv-成员已经存在的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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