簡體   English   中英

如何刪除csv文件的前兩行和第一列?

[英]How to remove first two rows and 1st column of a csv file?

我是Windows編程的新手。 我需要立即幫助來開發一個腳本,該腳本可以將CSV文件的前兩行和同一csv文件的第一數據列作為空白。 其余內容可以寫入另一個CSV文件。

第一排:總容量37,37,38

第二排:總票數1000

 , instrument, side, quantity
 , OK , OK, OK

歡迎使用帶有批處理文件或PowerShell的任何解決方案來完成此任務。

如果您願意使用PowerShell,這很簡單:

$content = Get-Content foo.csv
# Remove the first comma and whitespace from the file before further processing.
# I don't know how well the CSV parser likes an unnamed column
$content = $content -replace '^\s*,\s*'
# Parse CSV
$csv = ConvertFrom-Csv $content
# Write everything but the first two records back to the file
$csv | Select-Object -Skip 2 | Export-Csv foo.csv

但是請注意,您的輸入格式特別差的CSV。 幾乎在每個實現中都會保留字段值周圍的空格,並且在這方面您前后不一致。

最終,您可能想根據是否有值來過濾列或行。 在這種情況下,以上部分將變得有意義。

批處理非常簡單:

for /f "tokens=* delims=," %%i in ('more +2 file.csv') do echo %%i >>new.csv

(注意:空白行將被刪除)

這不會刪除現有的第一個令牌,但是您說它始終為空

與@Joey相同,但不刪除第一列

(gc C:\temp\csv.csv)  |Convertfrom-Csv |select instrument,side,quantity -Skip 2

暫無
暫無

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

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