簡體   English   中英

將csv文件拆分為指定數量的文件而無需分頁符

[英]Split csv file into specified number of files without page break

我有一個200,000個文件,可以使用powershell分成8個塊

該文件包含行,第一個值是記錄“ KEY”

我想確保在發生拆分時,與鍵字段值(該行的第一個值)相對應的行不會跨文件。

這是我使用的簡單拆分

$i=0
Get-Content -Encoding Default "C:\Test.csv" -ReadCount 10130 | ForEach-Object {
    $i++
    $_ | Out-File -Encoding Default "C:\Test_$i.csv"
}

樣本數據
0190709,HP16,B,B,3,3,
0190709,HP17,B,B,3,3,
0190709,HP18,B,B,3,3,
0196597,HP11,,CNN ,,,
0196597,HP119,,CNN ,,,
0196597,HP13,,CNN ,,,
01919769,HP11,,ANN ,,,
01919769,HP119,,OPN ,,,
01919769,HP13,,CNN ,,,
01919769,HP14,X,X,X,X,
01919769,HP15,A,A,X,X,
01919769,HP16,S,S,X,X,
01919769,HP17,S,S,5,5,
01919769,HP18,S,S,5,5,
0797819,HP14,X,AX,X,X,
0797819,HP15,X,XA,X,X,
0797819,HP16,X,X,XA,XA,
0797819,HP17,A,A,X,X,
0797819,HP18,A,A,AX,X,

預期產量

可以說我們想要2個大小相等的塊。 我想要2個以下文件,且密鑰不能在文件之間分割。 如果文件變大(行數更多)以防止密鑰分頁,則可以。

文件1

0190709,HP16,B,B,3,3,
0190709,HP17,B,B,3,3,
0190709,HP18,B,B,3,3,
0196597,HP11,,CaweNN ,,
0196597,HP119,,CNN ,,,
0196597,HP13,,CNwN ,,,
01919769,HP11,,AawNN ,,,
01919769,HP119,,OePN ,,,
01919769,HP13,,CNN ,,,
01919769,HP14,XY,X,X,X,
01919769,HP15,A,A,XC,XA,
01919769,HP16,S,S,X,X,
01919769,HP17,S,S,5A,5,
01919769,HP18,S,S,5,5,

文件2

0797819,HP14,X,AX,X,X,
0797819,HP15,X,XA,X,X,
0797819,HP16,X,X,XA,XA,
0797819,HP17,A,A,X,X,
0797819,HP18,A,A,AX,X,

盡管您沒有提供CSV文件的示例(前幾行),但以下功能假定輸入的csv文件有效。

function Split-Csv {
    [CmdletBinding()]  
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Path,          # the full path and filename of the source CSV file

        [Parameter(Mandatory = $true, Position = 1)]
        [string]$Destination,   # the path of the output folder

        [ValidateRange(1,[int]::MaxValue)]
        [int]$Chunks = 8,       # the number of parts to split into

        [switch]$FirstLineHasHeaders
    ) 
    # create the destination folder if it does not already exist
    if (!(Test-Path -Path $Destination -PathType Container)) {
        Write-Verbose "Creating folder '$Destination'"
        New-Item -Path $Destination -ItemType Directory | Out-Null
    }
    $outputFile = [System.IO.Path]::GetFileNameWithoutExtension($Path)
    $content    = Get-Content -Path $Path
    $totalLines = $content.Count

    if ($FirstLineHasHeaders) {
        $headers  = $content[0]
        $partsize = [Math]::Ceiling(($totalLines - 1) / $Chunks)
        for ($i = 0; $i -lt $Chunks; $i++) {
            $first   = ($i * $partsize + 1)
            $last    = [Math]::Min($first + $partsize -1, $totalLines - 1)
            $newFile = Join-Path -Path $Destination -ChildPath ('{0}-{1:000}.csv' -f $outputFile, ($i + 1))
            Write-Verbose "Creating file '$newFile'"
            Set-Content -Path $newFile -Value $headers -Force
            Add-Content -Path $newFile -Value $content[$first..$last]
        }
    }
    else {
        $partsize   = [Math]::Ceiling($totalLines / $Chunks)
        for ($i = 1; $i -le $Chunks; $i++) {
            $first   = (($i - 1) * $partsize)
            $last    = [Math]::Min(($i * $partsize) - 1, $totalLines - 1)
            $newFile = Join-Path -Path $Destination -ChildPath ('{0}-{1:000}.csv' -f $outputFile, $i)
            Write-Verbose "Creating file '$newFile'"
            Set-Content -Path $newFile -Value $content[$first..$last] -Force
        }
    }
}

如果您輸入的csv文件具有標題,則需要確保每個“塊”文件也都具有這些標題。 使用函數WITH開關$FirstLineHasHeaders

Split-Csv -Path 'C:\Test.csv' -Destination 'D:\test' -Chunks 8 -FirstLineHasHeaders -Verbose

如果您輸入的csv文件沒有標題,請按以下方式使用它:

Split-Csv -Path 'C:\Test.csv' -Destination 'D:\test' -Chunks 8 -Verbose

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM