如何访问Import-Csv数组中的特定行? [英] How To Access Specific Rows in an Import-Csv Array?

查看:93
本文介绍了如何访问Import-Csv数组中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个大文件上传拆分为多个并行进程,并希望使用单个CSV文件作为输入. 是否可以从Import-Csv对象访问行块,如下所示:

I need to split a large file upload into many parallel processes and want to use a single CSV file as input. Is it possible to access blocks of rows from an Import-Csv object, something like this:

$SODAData = Import-Csv $CSVPath -Delimiter "|" |
            Where $_.Rownum == 20,000..29,999 | 
            Foreach-Object { ... }

这种提取的语法是什么? 我正在使用Powershell 5.

What is the syntax for such an extraction? I'm using Powershell 5.

推荐答案

Import-Csv将文件作为对象数组导入,因此您可以执行以下操作(使用范围运算符):

Import-Csv imports the file as an array of objects, so you could do something like this (using the range operator):

$csv = Import-CSv $CSVPath -Delimiter '|'
$SOAData = $csv[20000..29999] | ForEach-Object { ... }

一种替代方法是使用Select-Object:

$offset = 20000
$count  = 10000
$csv = Import-Csv $CSVPath -Delimiter '|'
$SODAData = $csv |
            Select-Object -Skip $offset -First $count |
            ForEach-Object { ... }

如果要避免将整个文件读入内存,可以将以上内容更改为单个管道:

If you want to avoid reading the entire file into memory you can change the above to a single pipeline:

$offset = 20000
$count  = 10000
$SODAData = Import-Csv $CSVPath -Delimiter '|' |
            Select-Object -Skip $offset -First $count |
            ForEach-Object { ... }

但是请注意,使用这种方法需要多次读取文件才能处理多个数据块.

Beware, though, that with this approach you need to read the file multiple times for processing multiple chunks of data.

这篇关于如何访问Import-Csv数组中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆