2013-07-26 55 views
0

我在c#winforms中做了一個項目。如何使用c#在csv中獲取特定於值的列值?

我想在csv中獲得第一列值。

var reader = new StreamReader(File.OpenRead(@"C:\test.csv")); 
List<string> listA = new List<string>(); 
List<string> listB = new List<string>(); 
while (!reader.EndOfStream) 
{ 
var line = reader.ReadLine(); 
var values = line.Split(';'); 
listA.Add(values[0]); 

} 


------------------ 
no |name  | 

------------------ 
1  |wwwwww 
2  |yyyyy 
3  |aaaaa 
4  |bbbbbb 

現在我正在使用上面的這段代碼。它逐行給出值。我想要名單中的所有名字值A

任何人有想法?

+0

您已經獲得第一列,那麼問題是什麼? –

+0

您的要求不明確。你能舉一個輸入數據的例子,以及你想要輸出的數據的例子 – musefan

+0

現在輸出是第一行。我要第一列@TimSchmelter –

回答

1

現在有方法來讀取CSV列而不讀取整個文件。你可以使用一些包裝(例如:LINQ to CSV library),但他們只會「隱藏」閱讀操作。

1

是 - 您目前正在分手;

嘗試使用逗號代替。

最好使用BTW專用庫...

+0

我認爲這是正確的答案。 –

0
var reader = new StreamReader(File.OpenRead(@"C:\test.csv")); 
List<string> listA = new List<string>(); 

while (!reader.EndOfStream) 
{ 

var line = reader.ReadLine(); 

string[] dates = line.Split(',');   
for (int i = 0; i < dates.Length; i++) 
    { 
    if(i==0) 
    listA.Add(dates[0]); 
    } 


} 
1

在正則表達式有些皺眉,但是我認爲它提供了良好的柔韌性。這是一個由 Adrian Mejia啓發的例子。基本上,您可以選擇上下文中分隔符有效的特定字符。即「你好,世界」或「你好,世界」中的逗號是否有效。

static void Main(string[] args) 
    { 
     string csv = "Hello,1,3.5,25,\"speech marks\",'inverted commas'\r\nWorld,2,4,60,\"again, more speech marks\",'something else in inverted commas, with a comma'"; 

     // General way to create grouping constructs which are valid 'text' fields 
     string p = "{0}([^{0}]*){0}"; // match group '([^']*)' (inverted commas) or \"([^\"]*)\" (speech marks) 
     string c = "(?<={0}|^)([^{0}]*)(?:{0}|$)"; // commas or other delimiter group (?<=,|^)([^,]*)(?:,|$) 
     char delimiter = ','; // this can be whatever delimiter you like 
     string p1 = String.Format(p, "\""); // speechmarks group (0) 
     string p2 = String.Format(p, "'"); // inverted comma group (1) 
     string c1 = String.Format(c, delimiter); // delimiter group (2) 
     /* 
     * The first capture group will be speech marks ie. "some text, " 
     * The second capture group will be inverted commas ie. 'this text' 
     * The third is everything else seperated by commas i.e. this,and,this will be [this][and][this] 
     * You can extend this to customise delimiters that represent text where a comma between is a valid entry eg. "this text, complete with a pause, is perfectly valid" 
     * 
     * */ 
     //string pattern = "\"([^\"]*)\"|'([^']*)'|(?<=,|^)([^,]*)(?:,|$)"; 
     string pattern = String.Format("{0}|{1}|{2}", new object[] { p1, p2, c1 }); // The actual pattern to match based on groups 

     string text = csv; 

     // If you're reading from a text file then this will do the trick. Uses the ReadToEnd() to put the whole file to a string. 
     //using (TextReader tr = new StreamReader("PATH TO MY CSV FILE", Encoding.ASCII)) 
     //{ 
     // text = tr.ReadToEnd(); // just read the whole stream 
     //} 

     string[] lines = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); // if you have a blank line just remove it? 
     Regex regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); // compile for speed 

     List<object> rowsOfColumns = new List<object>(); 
     foreach (string row in lines) 
     { 
      List<string> columns = new List<string>(); 
      // Find matches. 
      MatchCollection matches = regex.Matches(row); 
      foreach (Match match in matches) 
      { 
       for (int ii = 0; ii < match.Groups.Count; ii++) 
       { 
        if (match.Groups[ii].Success) // ignore things that don't match 
        { 
         columns.Add(match.Groups[ii].Value.TrimEnd(new char[] { delimiter })); // strip the delimiter 
         break; 
        } 
       } 
      } 
      // Do something with your columns here (add to List for example) 
      rowsOfColumns.Add(columns); 
     } 
    } 
相關問題