如何使用 ForEach 迭代每一行 CSV 数据?
how to iterate each row of CSV data using ForEach?
希望在此处专门使用 ForEach
。
读入并加载示例数据的 csv
文件:
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a = Import-Csv -Path ./alphabet.csv
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a[7].psobject.Properties.name
symbol
code word
morse code
phonetic
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a[7].'code word'
Hotel
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a.'code word'
Alfa/Alpha
Bravo
Charlie
Delta
Echo
Foxtrot
Golf
Hotel
India
Juliett
Kilo
..
Yankee
Zulu
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $numbers=(1,2,3,4,5)
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> foreach ($number in $numbers){ echo $number}
1
2
3
4
5
PS /home/nicholas/powershell/regex>
但是如何使用 foreach
遍历每一行?类似于:
$a.forEach($code_word in $a.'code word'){echo $code_word}
希望在此处专门使用 ForEach
,最好是可以在 REPL 控制台中使用的一行。
Powershell 中有两个 Foreach 实例。一个是关键字,另一个是 Foreach-object 的别名。循环遍历 csv 文件的最简单方法是 import-csv,通过管道发送结果,并使用 Foreach-Object 引入循环。 Foreach-Object 的特殊符号“%”也是shorthand。
试试这个:
Import-csv -path myfile.csv |
Foreach-object {
$sym = $_.symbol
$code = $_.'code word'
"The code word for $sym is $code"
}
在这个 csv 文件上。
symbol,code word,morse code,phonetic
A,Alfa/Alpha,"● ▬",AL FAH
B,Bravo,"▬ ● ● ●",BRAH VOH
C,Charlie,"▬ ● ▬ ●",CHAR LEE
D,Delta,"▬ ● ●",DELL TAH
E,Echo,"●",ECK OH
F,Foxtrot,"● ● ▬ ●",FOKS TROT
G,Golf,"▬ ▬ ●",GOLF
Z,Zulu,"▬ ▬ ▬ ▬ ▬",ZOO LOO
H,Hotel,"● ● ● ●",HOH TELL
希望在此处专门使用 ForEach
。
读入并加载示例数据的 csv
文件:
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a = Import-Csv -Path ./alphabet.csv
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a[7].psobject.Properties.name
symbol
code word
morse code
phonetic
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a[7].'code word'
Hotel
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $a.'code word'
Alfa/Alpha
Bravo
Charlie
Delta
Echo
Foxtrot
Golf
Hotel
India
Juliett
Kilo
..
Yankee
Zulu
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> $numbers=(1,2,3,4,5)
PS /home/nicholas/powershell/regex>
PS /home/nicholas/powershell/regex> foreach ($number in $numbers){ echo $number}
1
2
3
4
5
PS /home/nicholas/powershell/regex>
但是如何使用 foreach
遍历每一行?类似于:
$a.forEach($code_word in $a.'code word'){echo $code_word}
希望在此处专门使用 ForEach
,最好是可以在 REPL 控制台中使用的一行。
Powershell 中有两个 Foreach 实例。一个是关键字,另一个是 Foreach-object 的别名。循环遍历 csv 文件的最简单方法是 import-csv,通过管道发送结果,并使用 Foreach-Object 引入循环。 Foreach-Object 的特殊符号“%”也是shorthand。
试试这个:
Import-csv -path myfile.csv |
Foreach-object {
$sym = $_.symbol
$code = $_.'code word'
"The code word for $sym is $code"
}
在这个 csv 文件上。
symbol,code word,morse code,phonetic
A,Alfa/Alpha,"● ▬",AL FAH
B,Bravo,"▬ ● ● ●",BRAH VOH
C,Charlie,"▬ ● ▬ ●",CHAR LEE
D,Delta,"▬ ● ●",DELL TAH
E,Echo,"●",ECK OH
F,Foxtrot,"● ● ▬ ●",FOKS TROT
G,Golf,"▬ ▬ ●",GOLF
Z,Zulu,"▬ ▬ ▬ ▬ ▬",ZOO LOO
H,Hotel,"● ● ● ●",HOH TELL