1

我只是一个新手,ai写了一个小脚本来生成一个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 文件。

谢谢

4

1 回答 1

6

我会使用 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

测试.tsv

"Property1" "Property2" "Property3"
"Val1o1"    "Val2o1"    "Val3o1"
"Val1o2"    "Val2o2"    "Val3o2"
"Val1o3"    "Val2o3"    "Val3o3"
于 2013-01-30T16:38:01.060 回答