2012-02-01 72 views
17

Tôi có mã sau để đọc các giá trị từ tệp csv và thực hiện một số thao tác. Tôi muốn bỏ qua hàng đầu tiên của tệp csv đầu vào vì nó chứa văn bản tiêu đề nhưng tôi muốn thêm nó trở lại sau khi xử lý xong.Cách bỏ qua dòng đầu tiên trong khi đọc csv bằng cách sử dụng trình streamreader

List<string> values = new List<string>(); 
using (StreamReader sr = new StreamReader(filePath)) 
{ 
    while (sr.Peek() != -1) 
    { 
     string line = sr.ReadLine(); 
     List<string> lineValues = line.Split(',').ToList(); 
     var tempMinInt = 1; 
     var tempValue = 1; 
     var tempValInt = Convert.ToInt32(lineValues[4]); 
     if (lineValues[3] == "1876") 
     { 
      if (tempValInt % 60 != 0) 
      { 
       tempMinInt = (tempValInt/60) + 1; 
       tempValue = tempMinInt * 30; 
      } 
      else 
      { 
       tempMinInt = tempValInt/60; 
       tempValue = tempMinInt * 30; 
      } 
     } 
     else if (lineValues[3] == "1875") 
     { 
      if (tempValInt != 0) 
      { 
       tempValue = 500; 
      } 
      else 
       tempValue = 0; 
     } 

     if (lineValues[3] == "1876") 
     { 
      values.Add(string.Join(",", lineValues) + "," + "0" + "," + "30" + "," + tempValue.ToString()); 
     } 
     else if (lineValues[3] == "1875") 
     { 
      values.Add(string.Join(",", lineValues) + "," + "1" + "," + "500" + "," + tempValue.ToString()); 
     } 

    } 
} 

đầu vào mẫu csv trông như thế này:

id, datetime, msisdn, num, duration 
33083,2011-12-19 05:17:57+06:30,98590149,1875,258 
33084,2011-12-19 05:22:28+06:30,98590149,1875,69 
33085,2011-12-19 05:23:45+06:30,98590149,1875,151 
33086,2011-12-19 05:30:21+06:30,98590149,1875,58 
33087,2011-12-19 06:44:19+06:30,949826259,1875,66 

Và tôi muốn có đầu ra của tôi như thế này:

id, datetime, msisdn, num, duration, type, ammount, total 
33083,2011-12-19 05:17:57+06:30,98590149,1875,258,1,500,500 
33084,2011-12-19 05:22:28+06:30,98590149,1875,69,1,500,500 
33085,2011-12-19 05:23:45+06:30,98590149,1875,151,1,500,500 
33086,2011-12-19 05:30:21+06:30,98590149,1875,58,1,500,500 

Trả lời

39

Chỉ cần đọc nó đầu tiên trước khi bạn nhận được vào vòng lặp . Tôi muốn làm điều này:

using (StreamReader sr = new StreamReader(filePath)) 
{ 
    string headerLine = sr.ReadLine(); 
    string line; 
    while ((line = sr.ReadLine()) != null) 
    { 
     ... 
    } 
} 

(tôi không thích sử dụng Peek, cá nhân.)

Sau đó, khi bạn viết ra đầu ra, bắt đầu với headerLine.

1

Bạn có thể đọc dòng đầu tiên trước vòng lặp while và lưu nó hoặc bạn có thể sử dụng trường đếm/boolean để kiểm tra vị trí của tệp.

11

Chỉ cần đọc những dòng đầu tiên và không làm gì với nó ...

List<string> values = new List<string>(); 
using (StreamReader sr = new StreamReader(filePath)) 
{ 
    sr.ReadLine(); 
    while (sr.Peek() != -1) 
    { 
     string line = sr.ReadLine(); 
     List<string> lineValues = line.Split(',').ToList(); 

     //***// 
    } 
} 
3

Something như thế này:

bool isFirst=true; 
using (StreamReader sr = new StreamReader(filePath)) 
{ 
    while (sr.Peek() != -1) 
    { 
     if(isFirst) 
     { 
      isFirst=false; 
      continue; 
     } 
    } 
} 
Các vấn đề liên quan