我只是一个新手,我写了一个小脚本来生成tsv txt文件。

该代码是

$("x")+ "`t" +("y") + "`t"+ $("z")| Add-Content C:\temp\DCO.txt
$i=0
$ts= Get-Date
while($i -le 1000000)
{
$x="N/A"
$y="N/A"
$z="N/A"
($x) + "`t"+ ($y)+ "`t"+ ($z)| Add-Content C:\temp\DCO.txt
$i++
}
$tf= Get-Date

花了很多时间。如果还有其他优化方法可以编写大小约为50mb或更多的tsv文件。

谢谢

最佳答案

我将使用Export-CSV来执行此操作。将值收集到对象并将其导出。例如:

#Create objects(think lines in a tsv) with properties and values
$o1 = New-Object psobject -Property @{
    Property1 = "Val1o1"
    Property2 = "Val2o1"
    Property3 = "Val3o1"
}

$o2 = New-Object psobject -Property @{
    Property1 = "Val1o2"
    Property2 = "Val2o2"
    Property3 = "Val3o2"
}

#Collect to array
$o = $o1, $o2

#Save to TSV
$o | Export-Csv "c:\test.tsv" -Delimiter `t -NoTypeInformation

#Add content
$o3 = New-Object psobject -Property @{
    Property1 = "Val1o3"
    Property2 = "Val2o3"
    Property3 = "Val3o3"
}

#Append to TSV-file
$o3 | Export-Csv "c:\test.tsv" -Delimiter `t -NoTypeInformation -Append

#To read file back to PowerShell objects
$a = Import-Csv "C:\test.tsv" -Delimiter `t

Test.tsv
"Property1" "Property2" "Property3"
"Val1o1"    "Val2o1"    "Val3o1"
"Val1o2"    "Val2o2"    "Val3o2"
"Val1o3"    "Val2o3"    "Val3o3"

关于windows - 在Windows Power Shell脚本中写入tsv文件的优化方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14608790/

10-13 09:33