0

These are the contents of a CSV file -

altitudeAscent,altitudeDescent,altitudeavg,altitudecurrent,altitudeend,altitudemax,altitudemin,altitudestart,calories,className,createAppVersion,createDevice,createIOSVersion,distance,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,startTime,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,Weight Lifting,85,gps,884,4.000000,TEMP-1663889272,37.10730362,-76.50557709,2013-08-26T18:48:21,110056,0,0

I need to delete the '7.00' value from the CSV file. How do I do that in VB or C#?

wackytacky99
  • 614
  • 1
  • 14
  • 33

3 Answers3

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);
Rob G
  • 3,496
  • 1
  • 20
  • 29
  • I just need to remove '7.00' not the Key. The problem is there are more values than keys. So I need to remove 7.00 so that all values after that match to the Keys. – wackytacky99 Oct 13 '13 at 23:09
1
  1. I would connect to the file using a connection string found here, or an example here, load it into a DataTable object, and then this (assuming you called the variable "dt"):

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

  2. If you always have it equal to 7.00, then you could read the file using TextReader, and then do something like yourFile = yourFile.Replace(",7.00,", ",,");

It depends on your scenario, maybe you could even get your data in a more suitable format for working, like a JSON object or something else.

Community
  • 1
  • 1
Eugene
  • 2,965
  • 2
  • 34
  • 39
1

This works for me

 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();
demo.b
  • 3,299
  • 2
  • 29
  • 29