我有一个看起来像这样的Csv:
$x
我首先需要做的是使用Netweight列进行一些数学计算得到两个值,$y
和$x
。
然后我需要将Csv中的每一行拆分为"AL,$y"
行数据,其中每行数据看起来都像$x
在下面的示例中,我成功获得了每行的$y
和$x
值。尝试将每行拆分为$fileContent = Import-Csv $File
$x = ( $fileContent | ForEach-Object { ( [System.Math]::Round( $_.Netweight /25000 ) ) } )
$y = ( $fileContent | ForEach-Object { $_.Netweight / ( [System.Math]::Round( $_.Netweight /25000 ) ) } )
$NumRows = ( $fileContent | ForEach-Object { (1..$x) } )
$rows = ( $NumRows | ForEach-Object { "Al,$y" } )
行时出现问题...:
$x = 8 and $y = 20
当Csv中有一行数据时,此代码可以正常工作。即如果"AL,20"
,它将返回8行数据,看起来像Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32".
。
但是,当Csv中有多行时,我收到一条错误消息:
euro <- "€"
eurosearch <- "services as defined in this SOW at a price of € 15,896.80 (if executed fro"
sub(paste0("^.*", gsub("([^A-Za-z_0-9])", "\\\\\\1", euro), "\\s*(\\S+).*"), "\\1", eurosearch)
euro <- "$"
eurosearch <- "services as defined in this SOW at a price of $ 25,196.4 (if executed fro"
sub(paste0("^.*", gsub("([^A-Za-z_0-9])", "\\\\\\1", euro), "\\s*(\\S+).*"), "\\1", eurosearch)
我希望我已经解释说好了,感谢任何帮助。 谢谢, 约翰
答案 0 :(得分:3)
而不是一遍又一遍地使用ForEach-Object
,只需迭代csv一次,然后一次生成一个$x
和$y
个结果:
$fileContent = @'
No,BundleNo,Grossweight,Pieces,Tareweight,Netweight
1,Q4021317/09,193700,1614,646,193054
2,Q4021386/07,206400,1720,688,205712
'@ | ConvertFrom-Csv
foreach($line in $fileContent){
$x = [System.Math]::Round( $line.Netweight / 25000 )
if($x -ne 0){
$y = $line.Netweight / $x
1..$x |ForEach-Object {"AL,$y"}
}
}
每行产生$x
个"AL,$y"
个字符串:
AL,24131.75
AL,24131.75
AL,24131.75
AL,24131.75
AL,24131.75
AL,24131.75
AL,24131.75
AL,24131.75
AL,25714
AL,25714
AL,25714
AL,25714
AL,25714
AL,25714
AL,25714
AL,25714