如何将多行CSV文件导入单行

时间:2019-01-15 12:35:34

标签: powershell csv powershell-v3.0

我需要解析具有最后一个多行列的CSV并将其转换为单行CSV。

示例:

Name,Department,Team,Task
"Jack","QA","AF","He need to work
He needs to update
He needs to plan"
"Sam","Dev","Retail","He need to work
He needs to update
He needs to plan"

预期结果是:

Name,Department,Team,Task
"Jack","QA","AF","He need to work.He needs to update.He needs to plan."
"Sam","Dev","Retail","He need to work.He needs to update.He needs to plan"

我需要为此使用PowerShell。

1 个答案:

答案 0 :(得分:2)

啊,既然您已经引用了CSV值,这应该不太困难。

# I have faked the input using a here-string, but in real life, you should use
# $csv = Import-Csv -Path <PATH TO THE CSV FILE>

$csv = @"
Name,Department,Team,Task
"Jack","QA","AF","He need to work
He needs to update
He needs to plan"
"Sam","Dev","Retail","He need to work
He needs to update
He needs to plan"
"@ | ConvertFrom-Csv

# convert all newlines to a full-stop dot and replace multiple spaces in the Task field to become a single space character
$csv | ForEach-Object {
    $_.Task = $_.Task -replace '[\r?\n]+', '. ' -replace '\s{2,}', ' '
    # if you just want to 'normalize' whitespaces like a browser does, use this instead.
    # $_.Task = $_.Task -replace '\s+', ' '
}

现在$csv变量保存以下数据:

Name Department Team   Task                                                 
---- ---------- ----   ----                                                 
Jack QA         AF     He need to work. He needs to update. He needs to plan
Sam  Dev        Retail He need to work. He needs to update. He needs to plan

接下来,使用分号作为分隔符写入更新的CSV文件

$csv | Export-Csv -Path '<PATH TO THE EXPORTED CSV FILE>' -NoTypeInformation -Delimiter ';'

希望有帮助


编辑


根据您的评论,我了解更多列可能包含换行符。 这是更新的脚本,如果文件中包含换行符,它将把文件中的任何字段变成单个字符串。

# Again, I have faked the input using a here-string, but in real life, you should use
# $csv = Import-Csv -Path <PATH TO THE CSV FILE>

$csv = @"
Name,Department,Team,Task
"Jack","QA","AF
XYZ","He need to work
He needs to update
He needs to plan"
"Sam","Dev","Retail
Sales","He need to work
He needs to update
He needs to plan"
"@ | ConvertFrom-Csv

# get an array of the header names
$headers = $csv[0].PSObject.Properties.name

$csv | ForEach-Object {
    foreach ($hdr in $headers) {
        # this regex converts all newlines to a full-stop dot and replaces multiple spaces to become one single space character
        $_.$hdr = $_.$hdr -replace '[\r?\n]+', '. ' -replace '\s{2,}', ' '
        # if you just want to 'normalize' all whitespaces like a browser does, use this instead.
        # $_.$hdr = $_.$hdr -replace '\s+', ' '
    }
}

此后,$csv变量将保存以下数据:

Name Department Team          Task                                                 
---- ---------- ----          ----                                                 
Jack QA         AF. XYZ       He need to work. He needs to update. He needs to plan
Sam  Dev        Retail. Sales He need to work. He needs to update. He needs to plan

照常导出到新的CSV文件:

$csv | Export-Csv -Path '<PATH TO THE EXPORTED CSV FILE>' -NoTypeInformation -Delimiter ';'