2013-10-13 43 views
0

值這是一個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,是,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

我需要從CSV文件中刪除'7.00'值。我如何在VB或C#中做到這一點?

回答

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); 
+0

我只需要刪除 '7.00' 並不是關鍵。問題是有更多的值比鍵。所以我需要刪除7.00,以便所有的值都與Keys匹配。 – wackytacky99

1
  1. 我將使用連接字符串發現here,或一個例子here連接到文件,將其加載到一個數據表對象,然後將此(假設你稱爲變量「DT」):

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

  2. 如果你總是有它等於7.00,那麼你可以閱讀使用TextReader的文件,然後像做yourFile = yourFile.Replace(",7.00,", ",,");

這取決於你的場景,也許你甚至可以讓你的數據更適合工作的格式,比如JSON對象或其他東西。

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();