How do I return a string from a csv file?

Hi,

I am trying to search a csv file until it hits a null value. The csv file is set up in such a way that I can search line by line until the first cell is blank (MB/Code). At the moment I just want to find this null cell and get the program to return the value, and then I can find the Res-Code and do some further calculations. Below is an example of the file I am searching.

MB/Code Description Res-Code

D desc1
D01 desc2
D0101 desc3
A001023


Thanks,
Scott


Answer this question

How do I return a string from a csv file?

  • ywchen

    Thanks Greg, I'll check that out.

  • jason jones

    I should've posted my code that I have written so far (the second part is what I am trying to sort out - readPath2)...

    using System;
    using System.IO;

    public class Reader
    {
    public static void Main()
    {
    string readPath = "Master-res2007.csv";
    string writePath = "Master-res2007.txt";
    string data;

    if(File.Exists(readPath))
    {
    StreamReader reader = new StreamReader(readPath);

    // Read to end of file
    string file = reader.ReadToEnd();

    StreamWriter writer = new StreamWriter(writePath);

    // Write to text file
    writer.WriteLine(file);

    reader.Close();
    writer.Close(); // Free resources

    }else{
    Console.WriteLine("The file: " + (readPath) + " could not be found");
    }

    string readPath2 = "D-Ground.csv";

    if(File.Exists(readPath2))
    {
    StreamReader reader2 = new StreamReader(readPath2);

    //Read single line of text
    data = reader2.ReadLine();

    int peek = reader2.Peek();


    //While loop to check for Res-Code

    reader2.Close();
    //writer2.Close(); // Free resources

    }
    }
    }

  • newyuppie

    This is a duplicate post and should probably be deleted.

    That said ...

    http://www.heikniemi.net/hc/archives/files/CSVReader.cs

    http://www.codeproject.com/cs/database/CsvReader.asp

    These are pre-done CSV parsing classes that should make this task a bit easier for you an I know atleast on of them also handles such things as quotes and commas in strings.

    Cheers,

    Greg


  • David d

    Peek returns the next available *character*.  Why can't you just read the line and parse it

     http://msdn2.microsoft.com/en-US/library/system.io.streamreader.peek(VS.80).aspx

    Your other option is to peek along the line, a character at a time, but I don't see the point.

     



  • Kimber6337

    I should be more clear. I want to read a line of text, and then return the code at the beginning of the next line.

  • ImpMacker

    Oh I am not suggesting you use them as a reference .. I am just suggesting you use them directly instead of spending the time to write your own as there are some intricacies of the CSV specification that can get tricky (namely strings can contain commas and require special handling)

    As for just looping through with readline .. there is an easier way.

    string read = "";
    using (StreamReader sr = new StreamReader(readPath2))
    {
    read = sr.ReadLine();
    while (read != null)
    {
    writer2.WriteLine(read);
    sr.ReadLine();
    }
    }

    you could make this a bit slimmer by inversin the loop but it will work.

    I highly recommend trying out those libraries as they will probably work better for you (and use less of your time on implementation)

    Cheers,

    Greg


  • Murat Soyupak

    I am still experiencing problems. The output is one line of text from the csv file, but the peek() method is still not working. This is what the code looks like at the moment:

    using System;
    using System.IO;

    public class Reader
    {
    public static void Main()
    {
    string readPath2 = "D-Ground.csv";
    //string writePath2 = "D-Ground.txt";

    if(File.Exists(readPath2))
    {
    StreamReader reader2 = new StreamReader(readPath2);
    string buffer = reader2.ReadLine();
    string[] split = null;
    split = buffer.Split(',');

    foreach (string s in split)
    {
    if (reader2.Peek() >= 0)
    {
    Console.WriteLine("[{0}]", s);
    }else{
    Console.WriteLine("null", s);
    }
    }
    }
    }
    }

  • Norse

    Couldn't understand the code at all :S

    I still have the same problem, however, I have progressed slightly. Here is the code I am having problems with (the Peek() method is not working):

    string readPath2 = "D-Ground-test.csv";
    string writePath2 = "D-Ground.txt";

    if(File.Exists(readPath2))
    {
    StreamWriter writer2 = new StreamWriter(writePath2);
    using (StreamReader sr = new StreamReader(readPath2))
    {
    while (sr.Peek() >= 0)
    {
    writer2.WriteLine(sr.ReadLine());
    }
    }
    }

  • sinewave

    Thanks for the reply. I can get the next line fine, but I want to return the next string. I've tried many different functions such as 'peek()', but to no avail.

    if(File.Exists(readPath2))
    {
    StreamReader reader2 = new StreamReader(readPath2);

    //Read single line of text
    data = reader2.ReadLine(); --- That's what I'm doing here



  • Mike Q

    Thanks, I will try that.

  • Leonid_

    what is

    foreach (string s in split)
    {
    if (reader2.Peek() >= 0)
    {
    Console.WriteLine("[{0}]", s);
    }else{
    Console.WriteLine("null", s);
    }
    }

    trying to do Why are you peeking at the file for every string in the previous line


  • Athan00

    If you use ReadLine to read a line at a time instead of ReadToEnd, you can look for the line you're after as you go.



  • How do I return a string from a csv file?