如何将一行分成多行? [英] How to split a line into multiple lines?
问题描述
我有一个CSV文件,看起来像这样:
I have a CSV file which looks something like this:
Column1,Column2,Column3
John,Smith,"AA, AH, CA, NI, PB"
Reginald,Higginsworth,"AA, AH, CA, NI, PB, SN, ZS"
您会注意到 Column3中有多个值
(用引号括起来)。
我需要做的是在CSV中为 Column3
中的每个值生成一行数据,即该文件将需要看起来像: / p>
You'll notice that there are multiple values within Column3
(enclosed in the quotes).
What I need to do is produce a line of data in the CSV for each of these values in Column3
, i.e. the file would need to look something like:
Column1,Column2,Column3
John,Smith,AA
John,Smith,AH
John,Smith,CA
John,Smith,NI
John,Smith,PB
Reginald,Higginsworth,AA
Reginald,Higginsworth,AH
Reginald,Higginsworth,CA
Reginald,Higginsworth,NI
Reginald,Higginsworth,PB
Reginald,Higginsworth,SN
Reginald,Higginsworth,ZS
我已经做了多次尝试,似乎没有正确。
I have had more than several attempts at this and can't seem to get it right.
推荐答案
使用 Import-Csv
读取输入文件,然后拆分第三列并创建新的输出行每个结果元素:
Use Import-Csv
to read the input file, then split the third column and create new output lines for each resulting element:
Import-Csv 'C:\path\to\input.csv' | ForEach-Object {
foreach ($value in ($_.Column3 -split ', ')) {
$_ | Select-Object -Exclude Column3 -Property *,@{n='Column3';e={$value}}
}
} | Export-Csv 'C:\path\to\output.csv' -NoType
Select-Object
结构用新属性替换现有属性
Column3
$ c>只包含一个值。
The Select-Object
construct replaces the existing property Column3
with a new property Column3
that contains only a single value.
这篇关于如何将一行分成多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!