我一直在尝试更改我公司每月收到的 csv 发票。但是,该文件的大小为 1.5 GB,包含超过 1,500,000 行和 36 列。虽然我可以在 Microsoft Excel 中打开该文件,但尝试编辑数据会导致大多数行/列在我不知情的情况下被删除。尝试进入并编辑它会导致我丢失大约 16MB 的数据。我编写了一个 PowerShell 脚本将文件拆分为两个不同的文件,但问题是其中一个 CSV 缺少标头,我必须手动添加它。我也遇到了相同的问题,即某些数据被删除尽管低于 Excel 的 1,048,576 行限制。
我尝试在Visual Studio Code 中更新 CSV 并使用 Rainbow CSV 扩展,但我每次尝试打开 CSV 时都会看到 Visual Studio 崩溃。我也尝试过使用 Notepad++ 和使用 CSV Lite 插件,但是没有过滤选项来使操作更容易。我想知道是否有人对编辑这么大的 CSV 文件有建议,或者他们是否知道如何在 Excel 中编辑数据而不会有数据被意外删除的风险? 谢谢!
在
代码:
# Define the path to the CSV file
$csvPath = "C:\Users\yourfile.csv" # Replace 'yourfile.csv' with your actual CSV file name
# Load the CSV into a variable
$csvData = Import-Csv -Path $csvPath
# Iterate through each row in the CSV
foreach ($row in $csvData) {
# Check if any column contains "SubscriptionName1" and update the "CostCenter" column to "####"
if ($row.PSObject.Properties.Value -contains "SubscriptionName1") {
$row."CostCenter" = "####"
}
# Check if any column contains "SubscriptionName2" and update the "CostCenter" column to "####"
elseif ($row.PSObject.Properties.Value -contains "SubscriptionName2") {
$row."CostCenter" = "####"
}
elseif ($row.PSObject.Properties.Value -contains "SubscriptionName3") {
$row."CostCenter" = "####"
}
}
# Export the updated CSV
$csvData | Export-Csv -Path $csvPath -NoTypeInform
感谢所有贡献者!