如何解决我想创建一个文件,该文件将带有1.csv的标头并保留来自2.csv的所有数据在Powershell中
1.csv --->仅标头,2.csv ---> 8 GB文件。 从下面的编码中,我在100000行之后再次获得标头,并且某些数据也丢失了。
$process_Dir = 'E:\Pushpendra\HIPPA_File\'
(Get-Content $process_Dir\1.csv | Select -First 1 | Out-File -Append $process_Dir\new.csv -Encoding utf8)
(Get-Content $process_Dir\2.csv -ReadCount 100000) |Select-Object -Skip 1 | Out-File -Append $process_Dir\new.csv -Encoding utf8
解决方法
由于您的第二个CSV文件显然很大,因此建议您使用switch -File
读取文件以提高性能,并建议使用StreamWriter对象写入数据行。
类似这样的东西:
$process_Dir = 'E:\Pushpendra\HIPPA_File'
# create a new file with just the first line (the column headers) from '1.csv'
Get-Content -Path "$process_Dir\1.csv" -TotalCount 1 |
Set-Content -Path "$process_Dir\new.csv" -Encoding UTF8 -Force
# next,add the data lines from the second csv file to this
# because this is a very large file,we're using a streamwriter to add the lines
$stream = [System.IO.StreamWriter]::new("$process_Dir\new.csv",$true) # $true means append
$skipLine = $true # a boolean variable to skip the header line in 2.csv
switch -File "$process_Dir\2.csv" {
default { if ($skipLine) { $skipLine = $false } else { $stream.WriteLine($_) } }
}
# dispose the StreamWriter object
$stream.Flush()
$stream.Dispose()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。