在CSV中查找和删除值

时间:2013-10-13 22:55:16

标签: c# asp.net vb.net csv

这些是CSV文件的内容 -

altitudeAscent,altitudeDescent,altitudeavg,altitudecurrent,altitudeend,altitudemax,altitudemin,altitudestart,卡路里,类名,createAppVersion,CreateDevice的,createIOSVersion,距离,eaType,ecomodule,elapsedSeconds,fatCalories,footpodElapsedSecondsInterval,gpsSensor,gps_distance,gps_maxspeed,heartSensor,isCompletedAssessment ,isLocationIndoors,isgpsonly,maxBPM,MAXSPEED,:MINBPM,otherWorkoutTypeName,otherzonetimes,primarySourceSpeedDistance,readCountBPM,routineds,routineid,startLat,startLon,开始时间,totalBeats,totalDurationSeconds,totalPausedSeconds 0,2.37,-999999,33.66,33.66,42.76,28.21,36.47,410.08,ActiveWorkout, 7.00 下,iPhone5,1,7.0,0.27,0,1024,2657 ,207.77,2655,ios,0.27,3.69,ble,NO,NO,YES,186,3.69,87,举重,85,全球定位系统,884,4.000000,TEMP-1663889272,37.10730362,-76.50557709,2013-08-26T18 :48:21,110056,0,0

我需要从CSV文件中删除“7.00”值。我如何在VB或C#中做到这一点?

3 个答案:

答案 0 :(得分:3)

string names;
List<string> values;
using (var stream = new StreamReader("path/to/file.csv"))
{
    names = stream.ReadLine();
    values = stream.ReadLine().Split(',').ToList();
}
values.RemoveAt(9);
names; // first part of file
string secondPartOfFile = string.Join(",", values);

答案 1 :(得分:1)

  1. 我将使用找到的连接字符串here或示例here连接到该文件,将其加载到DataTable对象中,然后这个(假设您调用了变量“dt”) “):

    dt["columnName"][rowNumber] = string.Empty;

  2. 如果您总是等于7.00,那么您可以使用TextReader读取文件,然后执行yourFile = yourFile.Replace(",7.00,", ",,");

  3. 之类的操作

    这取决于您的场景,也许您甚至可以使用更合适的格式来处理数据,例如JSON对象或其他内容。

答案 2 :(得分:1)

这对我有用

 var sr = new StreamReader("file.csv");
            var writeToFile = new StreamWriter("out.csv");
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                writeToFile.WriteLine(sr.ReadLine().ToString().Replace(",7.00", ""));
            }
            writeToFile.Close();
            sr.Close();