开发者

C# Why do I only get partial results when parsing out a CSV or TSV file?

开发者 https://www.devze.com 2023-03-25 03:58 出处:网络
I am trying to get the second value from a CSV file with 100 rows.I am getting the first 42 values then it stops... no error messege, or error handling at all for that matter.I am perplexed and am on

I am trying to get the second value from a CSV file with 100 rows. I am getting the first 42 values then it stops... no error messege, or error handling at all for that matter. I am perplexed and am on a timeline. It is also doing it for a TSV file, but giving the first 43 results. Please help and let me know if it looks strange to you.

I am using streamreader, reading each line into a string array, splitting the array and taking the second value and adding it to a list...

        string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
        StreamReader sr = new StreamReader(path);
        List<string> stkno = new List<string>();

        foreach (var line in path)
        {
            string s = sr.ReadLine();
            string[] words = s.Split(',');
            stkno.Add(words[1]);
        }
        var message = string.Join(",", s开发者_Python百科tkno.ToArray());
        MessageBox.Show(message);


Your path variable is a string. That means when you foreach over it, you're getting a sequence of characters - 'C' then ':' then '\' etc. I don't think that's what you mean to do...

Here's a simpler approach using File.ReadLines:

string path = @"C:\Users\dave\Desktop\codes\testfile.txt";

List<string> stkno = (from line in File.ReadLines(path)
                      let words = line.Split(',')
                      select words[1]).ToList();

Or:

string path = @"C:\Users\dave\Desktop\codes\testfile.txt";

List<string> stkno = File.ReadLines(path)
                         .Select(line => line.Split(',')[1])
                         .ToList();

If you're using .NET 3.5 and you don't mind reading the whole file in one go, you can use File.ReadAllLines instead.


You are accidentally iterating over the number of characters in the file path instead of the number of lines in the string. This change should fix that:

    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    StreamReader sr = new StreamReader(path);
    List<string> stkno = new List<string>();

    while (sr.Peek() >= 0) 
    {
        string s = sr.ReadLine();
        string[] words = s.Split(',');
        stkno.Add(words[1]);
    }
    var message = string.Join(",", stkno.ToArray());
    MessageBox.Show(message);


How about this:

    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    var secondWords = from line in File.ReadAllLines(path)
                        let words = line.Split(',')
                        select words[1];

    var message = string.Join(",", secondWords.ToArray());


I think you mean to do:

    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    StreamReader sr = new StreamReader(path);
    List<string> stkno = new List<string>();

    string s;
    while(s = sr.ReadLine() != null)
    {
        string[] words = s.Split(',');
        stkno.Add(words[1]);
    }
    var message = string.Join(",", stkno.ToArray());
    MessageBox.Show(message);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号