1

is it possible to read csv file based on line. Like give a line number and it retrieves that particular line in CSV.

Thanks

kumar
  • 8,207
  • 20
  • 85
  • 176
  • 1
    Stop reading when *given* line number matches. – KV Prajapati Sep 17 '12 at 13:23
  • Not sure what your motivation is - if it's performance, consider using some DB... a CSV is not a DB, it is just a big file... to read a specific line number you will need to read all preceding lines (though you don't need to parse them completely)... – Yahia Sep 17 '12 at 13:27

5 Answers5

2

If you just want to read a line of a text file(like csv), the easiest approach is using File.ReadLines and Enumerable.ElementAtOrDefault:

public static String getFileLine(String path, int indexOfLine)
{
    return File.ReadLines(path).ElementAtOrDefault(indexOfLine);
}

Use it in this way:

String line = getFileLine(@"C:\Temp\CsvFile.csv", 99);

That returns the 100th line (or null if there are less than 100 lines).

Here's another similar method that returns a range of lines:

public static IEnumerable<String> getFileLines(String path, IEnumerable<int> lineIndices)
{
    return File.ReadLines(path).Where((l, i) => lineIndices.Contains(i));
}

return the first 10 lines:

IEnumerable<int> range = Enumerable.Range(0,10);
IEnumerable<String> lines = getFileLines(@"C:\Temp\CsvFile.csv", range);
foreach (String line in lines)
    Console.WriteLine(line);

Note that File.ReadLines is similar to a StreamReader, it does not read the whole file at once into memory(as FileReadAllLines), just as much as necessary.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • When using ElementAtOrDefault or ElementAt... is it possible to grab not just one line but several lines of csv? Like from Lines 20-25 for example? THanks – Kala J May 21 '14 at 11:45
  • @KalaJ: yes, you just have to use `Skip` and `Take`, for example: `var lines = File.ReadLines(path).Skip(19).Take(6);` ( 20-25 -> 6 lines ). It'll not even raise an exception if the file has less than 25 lines, it will just return what is there or `Enumerable.Empty` if there are less than 20 lines. – Tim Schmelter May 21 '14 at 11:47
  • @KalaJ: ... Use `ToList` at the end to create a `List`, otherwise you'll get problems with `File.ReadLines` which disposes the underlying stream on the first use (as opposed to `File.ReadAllLines` which returns an array). – Tim Schmelter May 21 '14 at 11:52
  • I have a question, the values that are comma separated... e.g. Apples, Oranges, Grapes.... The list would have three slots for each comma delimited value right and not just one big string? – Kala J May 21 '14 at 12:10
  • I keep getting this error: System.Collections.Generic.List`1[System.String] after I run something like MediansTable = File.ReadLines(filePath, Encoding.Default).Skip(41).Take(23).ToList(); Console.WriteLine(MediansTable); where MediansTable is List MediansTable – Kala J May 21 '14 at 12:17
  • If you want to write all values in a list to the console you can use `string.Join`: `Console.Write(string.Join(",",MediansTable))`. But please avoid asking questions in comments next time. Therefore ask a real question ;-) – Tim Schmelter May 21 '14 at 12:19
  • I will try that but here's my original question. I think this method you mentioned would be best and easiest for me to do instead of Regex. http://stackoverflow.com/questions/23763992/how-can-i-grab-comma-delimited-values-that-appear-after-a-term-i-searched-for – Kala J May 21 '14 at 12:22
0

Please use System.IO.StreamReader and System.IO.StreamWriter will work for reading and and writing to a csv file. They have ReadLine() and WriteLine() methods.

Anish V
  • 673
  • 3
  • 17
  • 38
0

It depends on your goal here but you should probably do something like

   while((line = readFile.ReadLine()) != null)
   {
   //Record line
   }

Then once you've read the files data into memory you can consider querying by line.

You really should have considered googling this by the way, even Bing'ing it would have got you an answer.

JDandChips
  • 9,780
  • 3
  • 30
  • 46
0

If your file is not very big you can do this:

string line10 = File.ReadAllLines(@"<filename>", Encoding.Default).ElementAt(10);
flayn
  • 5,272
  • 4
  • 48
  • 69
0

Not sure what your motivation is - if it's performance, consider using some DB... a CSV is not a DB, it is just a big file... to read a specific line number you will need to read all preceding lines (though you don't need to parse them completely)... OR read the whole file into memory File.ReadAllLines - this gives you a string[] which you can access at any line you want... OR use File.ReadLines which gives you IEnumerable<string> which might be more memory-efficient/performant with big files...

Yahia
  • 69,653
  • 9
  • 115
  • 144