csv - powershell csv 选择每第 n 行

标签 csv powershell

我正在使用 Powershell 处理包含多行坐标值的 CSV 文件。

第 1 行和第 2 行是我已转换为经纬度值的弧度。

我想出了下面有效的脚本。

我的问题是我不想要每一行——太多了。我想提取每 1000 行。

foreach ($FileName in get-item $dir\*.* -include test1.csv )
{
$FilenameNP = $Filename | select  -expand BaseName
Write-Host $FilenameNP
Write-Output "Latitude,Longitude,Altitude,TimeSeconds,LatitudeRadians,LongitudeRadians,AltitudeMeters,x velocity meters/second ,y velocity meters/second ,z velocity meters/second ,Roll radians ,Pitch radians ,platform heading radians ,wander angle radians ,x body acceleration meters/second2 ,y body acceleration meters/second2 ,z body acceleration meters/second2 ,x body angular rate radians/second ,y body angular rate radians/second ,z body angular rate radians/second " > $dir\Sbet_$FilenameNP.txt


$Content = get-content $Filename

Write-host $Content


foreach ($Line in $Content)
    {
    $Latitude=$null
    $Longitude=$null
    $Line=$Line -replace '\s+',''
    $LineS=$Line  -split ","
    $Latitude=([decimal]$LineS[1]*180/[math]::pi)
    $Longitude=([decimal]$LineS[2]*180/[math]::pi)
    $Altitude=$LineS[3]
    Write-Host $Latitude $Longitude $Altitude

    write-Output "$Latitude,$Longitude,$Altitude,$Line" >> $dir\Sbet_$FilenameNP.txt

  }
}

数据看起来像这样:

385278.0020318,     -0.6458227,      3.0509169,     39.0372952,      0.0044346,      0.0046028,
385278.0070309,     -0.6458227,      3.0509169,     39.0373095,      0.0036458,      0.0019423,
385278.0120310,     -0.6458230,      3.0509170,     45.1586564,      0.0025192,     -0.0011160,
385278.0170301,     -0.6458230,      3.0509170,     45.1586851,      0.0013969,     -0.0034220,
385278.0220292,     -0.6458230,      3.0509170,     45.1587176,      0.0002427,     -0.0041081,
385278.0270284,     -0.6458230,      3.0509170,     45.1587510,     -0.0006602,     -0.0027870,
385278.0320285,     -0.6458230,      3.0509170,     45.1587844,     -0.0012237,     -0.0001119,

最佳答案

如果您不想要每一行,请不要遍历每一行...使用 For 循环并让它迭代您想要提取的行数...

foreach ($FileName in get-item $dir\*.* -include test1.csv )
{
    $FilenameNP = $Filename | select  -expand BaseName
    Write-Host $FilenameNP
    Write-Output "Latitude,Longitude,Altitude,TimeSeconds,LatitudeRadians,LongitudeRadians,AltitudeMeters,x velocity meters/second ,y velocity meters/second ,z velocity meters/second ,Roll radians ,Pitch radians ,platform heading radians ,wander angle radians ,x body acceleration meters/second2 ,y body acceleration meters/second2 ,z body acceleration meters/second2 ,x body angular rate radians/second ,y body angular rate radians/second ,z body angular rate radians/second " > $dir\Sbet_$FilenameNP.txt

    $Content = get-content $Filename

    Write-host $Content
    For($i = 0;$i -lt $Content.count;$i=$i+1000){
        $Latitude=$null
        $Longitude=$null
        $Line=$Content[$i] -replace '\s+',''
        $LineS=$Line  -split ","
        $Latitude=([decimal]$LineS[1]*180/[math]::pi)
        $Longitude=([decimal]$LineS[2]*180/[math]::pi)
        $Altitude=$LineS[3]
        Write-Host $Latitude $Longitude $Altitude

        write-Output "$Latitude,$Longitude,$Altitude,$Line" >> $dir\Sbet_$FilenameNP.txt
    }
}

关于csv - powershell csv 选择每第 n 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31061871/

相关文章:

powershell - 每隔一天跳过两个午夜日期的 New-TimeSpan

Powershell v2.0 模块 : Default Load Path (User/Windows system folder)?

PowerShell 哈希表值从循环外的第二个哈希表中删除

php - 不使用 phpExcel 将大型 xlsx 文件转换为 csv

python - 反复从 Python 中读取 CSV?

mysql - 加载 Excel 文件 mySQL 时未找到结果

Python3 - CSV 编写器按日期排序

Powershell跨模块访问调用者范围变量

ios - 快速初始化 CHCSVPaser

powershell - 如何将条目添加到自定义对象数组