简体   繁体   English

从csv文件中特定列下的行中删除值

[英]Remove values from rows under specific columns in csv file

I'm currently trying to remove specific values from rows under specific columns in a CSV-file. 我目前正在尝试从CSV文件中特定列下的行中删除特定值。

Whats the best way of doing this? 最好的方法是什么?

Is it to use a XSLT map file in the code or doing this only by code? 是在代码中使用XSLT映射文件还是仅通过代码执行此操作? (Using c#) (使用C#)

What I want to do is this: 我想做的是这样的:

BEFORE MANIPULATION: 操作之前:

 id, name, email, phoneNumber, dob 
 1,John Doe,JohnDoe@mail.com,123456789,1988-08-08
 2,Jane Doe,JaneDoe@mail.com,987654321,1987-07-07

AFTER MANIPULATION: 操作后:

 id, name, email, phoneNumber, dob 
 1,John Doe,,,1988-08-08 
 2,Jane Doe,,,1987-07-07

As you can see "email" and "phoneNumber" is gone 如您所见,“电子邮件”和“ phoneNumber”都不见了

Best way very much depends on personal preferences. 最佳方式很大程度上取决于个人喜好。 For me the best way would be to use sed. 对我而言,最好的方法是使用sed。 Assuming your data is in data.csv file: 假设您的数据在data.csv文件中:

cat data.csv | sed '2,$ s/\([^,]*\),\(.[^,]*\),\(.[^,]*\),\([^,]*\),\([^,]*\)/\1,\2,,,\5/' > output.csv

You can use C# without any libs to separate and joint csv strings. 您可以使用不带任何库的C#来分隔和联合csv字符串。 it's easier than use XLST. 比使用XLST更容易。 As sample: 作为样本:

 String csv = "1,John Doe,JohnDoe@mail.com,123456789,1988-08-08";
 String[] csvList = csv.Split(',');
 csvList[2] = "";
 csvList[3] = "";
 csv = String.Join(",", csvList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM