合并CSV文件 [英] Merging CSV files

查看:235
本文介绍了合并CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个CSV档案合并为一个大型CSV档案。我写了一个powershell脚本,为每个标签名称成功创建单独的csv文件。但是当最后添加 Get-Content 时,会出现以下错误:

I am trying to merge a number of CSV files into a large CSV file. I wrote a powershell script that successfully create individual csv files for each tag names. But when added Get-Content at the end, I get this error:

Get-Content : An object at the specified path C:\HTD2CSV\Output_Files\*.CSV does not exist, or has been filtered by the
 -Include or -Exclude parameter.
At C:\HTD2CSV\extract.ps1:30 char:20
+ $temp = Get-Content <<<<  "$destinationPath\*.CSV" #| Set-Content $destinationPath\merged.CSV
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

但我已经在Output_Files文件夹中有CSV文件。在命令行上输入 Get-Content .\Output_Files \ * .CSV 工作正常,但显然不在脚本中。我的代码如下所示:

But I already have CSV files in the Output_Files folder. Entering Get-Content .\Output_Files\*.CSV worked fine on the command line, but apparently not in the script. My code looks like this:

$currentPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$sourcePath = "C:\Program Files (x86)\Proficy\Proficy iFIX\HTRDATA"
$destinationPath = "$currentPath\Output_Files"

    @(
        "PPP_VE0963A",
        "PPP_VE0963B",
        "PPP_VE0964A",
        "PPP_VE0964B",
        "PPP_VE0967A",
        "PPP_VE0967B",
        "PPP_ZE0963A",
        "PPP_ZE0963B",
        "PPP_ZE0964A",
        "PPP_ZE0964B"
    ) | ForEach-Object {
        .\HTD2CSV.exe `
            PPP:$_.F_CV `
            /dur:00:23:59:00 `
            /int:00:01:00 `
            /sd:05/01/14 `
            /st:00:00:00 `
            /sp:$sourcePath `
            /dp:$destinationPath\$_.CSV `
            /dtf:0 `
            /dbg:0
    }

Get-Content "$destinationPath\*.CSV" | Set-Content "$destinationPath\merged.CSV"


推荐答案

不要使用合并CSV的 Get-Content / Set-Content (假设您实际上有CSV和不只是特别命名的平面文本文件)。使用 Import-Csv Export-Csv

Don't use Get-Content/Set-Content for merging CSVs (assuming that you actually do have CSVs and not just peculiarly named flat text files). Use Import-Csv and Export-Csv:

Get-ChildItem '*.csv' | % {
  Import-Csv $_.FullName | Export-Csv 'C:\path\to\merged.csv' -NoType -Append
}


b $ b

或这样(以避免追加):

or like this (to avoid appending):

Get-ChildItem '*.csv' | % { Import-Csv $_.FullName } |
  Export-Csv 'C:\path\to\merged.csv' -NoType

这篇关于合并CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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