English 中文(简体)
如何在电壳中处理大型 CSV 文件
原标题:How to process large CSV file in powershell

我正在试图在 Csv 文件的行数中找到超过一定值的行数。

$T6=Import-Csv $file | Where-Object {$_."Value" -ge 0.6 } | Measure-Object

这对较小的文件非常有效, 但对于大 Csv 文件( 1 GB 或更多), 它会永远运行 。 在 powershell 中, 是否有更好的方法来解析这样的 csv 文件?

问题回答

导入 Csv 是此内容的官方 cmdlet 。 但有一个评论是, 所有导入的都是字符串, 所以您最好将“ 价值” 属性投向正确的类型 。 例如 :

$T6 = Import-Csv $file | Where-Object { [float]$_.Value -ge 0.6 } | Measure-Object

您可以尝试除去 Import-Csv :

$values = ([System.IO.File]::ReadAllText( c:pstNew Microsoft Office Excel Worksheet.csv )).Split(";") | where {$_ -ne ""}

$items = New-Object "System.Collections.Generic.List[decimal]" 

foreach($value in $values)
{
    [decimal]$out = New-Object decimal
    if ([System.Decimal]::TryParse($value, [ref] $out))
      {
         if ($out -ge 10){$items.Add($out)}
      } 
   }
$items | Measure-Object

为了快速处理大型文件时考虑使用溪流阅读器, Roman s ans answer





相关问题
Styling rows in table that uses CSV data through PHP

I ve been working on this simple code that puts a CSV file into a nice table. But because the data is imported, styling ODD rows is a pretty hard thing to do. All I need would be a method to address ...

PHP - Sanitise a comma separated string

What would be the most efficient way to clean a user input that is a comma separated string made entirely on numbers - e.g 2,40,23,11,55 I use this function on a lot of my inputs function clean($...

marking duplicates in a csv file

I m stumped with a problem illustrated in the sample below: "ID","NAME","PHONE","REF","DISCARD" 1,"JOHN",12345,, 2,"PETER",6232,, 3,"JON",12345,, 4,"PETERSON",6232,, 5,"ALEX",7854,, 6,"JON",12345,, ...

Interactive heat map in Flex

I’m at a very basic level with Flex and with programming in general. I am working on a project where I have data in an Excel (.csv) format, it’s a large Excel plot/matrix where each cell has a ...