patterncsharpMinor
CSV parsers and OrderedDictionary
Viewed 0 times
andordereddictionarycsvparsers
Problem
I wrote the following (static) class to read files easily in C#. However, it is a static class, so I wonder if there would be a gain to make it a non static method (and therefore, passing arguments as the separator and the path as attributes of the class).
Besides, the
```
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
namespace MySolution.FileUtils
{
///
/// Various helpers to read through the lines of a file.
///
public static class LinesEnumerator
{
public static string ReadFirstLine(string path)
{
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs))
{
return sr.ReadLine();
}
}
///
/// Enumerates the lines of a file.
///
/// The path of the file.
/// The maximum number of lines to read.
/// The lines of a file, as a IEnumerable
public static IEnumerable LinesReader(string path, int maxLines = Int32.MaxValue)
{
string line;
int lineNumber = 0;
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs))
{
while ((line = sr.ReadLine()) != null && lineNumber
/// Reads the lines of a CSV file and enumerates them through a csv file
///
/// Path to the CSV file
/// Separator used by the CSV file
/// A collection of ordered dictionaries, corresponding to the lines of the file
public static IEnumerable DictReaderCSV(string path, char separator)
{
using (FileStream f
Besides, the
OrderedDictionary class may not be a good choice (it is not generic but it preserves the order of the columns), and I don't know if there is a nice workaround for this.```
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
namespace MySolution.FileUtils
{
///
/// Various helpers to read through the lines of a file.
///
public static class LinesEnumerator
{
public static string ReadFirstLine(string path)
{
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs))
{
return sr.ReadLine();
}
}
///
/// Enumerates the lines of a file.
///
/// The path of the file.
/// The maximum number of lines to read.
/// The lines of a file, as a IEnumerable
public static IEnumerable LinesReader(string path, int maxLines = Int32.MaxValue)
{
string line;
int lineNumber = 0;
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs))
{
while ((line = sr.ReadLine()) != null && lineNumber
/// Reads the lines of a CSV file and enumerates them through a csv file
///
/// Path to the CSV file
/// Separator used by the CSV file
/// A collection of ordered dictionaries, corresponding to the lines of the file
public static IEnumerable DictReaderCSV(string path, char separator)
{
using (FileStream f
Solution
Naming Items
You should avoid abbreviations like
Next, it's much more natural to use nouns for class names and verbs for methods. As such, I would name your class
Unused code
Streams
The
What would however give much more flexibility is for the class to allow the user to provide it with an already constructed
Parsing CSV
CSV files allow for quotes to be used so that entries can contain the separator. A parser should be prepared to correctly deal with this.
You should avoid abbreviations like
Dict in DictReaderCSV, spelling it Dictionary is cleaner.Next, it's much more natural to use nouns for class names and verbs for methods. As such, I would name your class
CSVDictionaryReader and your main method EnumerateEntries(). LinesEnumerator didn't give any hint that the class is also responsible for parsing CSV.Unused code
ReadFirstLine and ReadFirstLine don't seem to be used in the current code.Streams
The
StreamReader class already provides constructors that receive a disk path, so no need to manually create a FileStream just to pass it onto the StreamReader.What would however give much more flexibility is for the class to allow the user to provide it with an already constructed
Stream or TextReader. Perhaps the user wants to parse a 100 MB CSV as it's arriving over the wire, without writing it to the disk.Parsing CSV
CSV files allow for quotes to be used so that entries can contain the separator. A parser should be prepared to correctly deal with this.
Id,Name,Age
1,John,20
2,"Jack, the second", 21Code Snippets
Id,Name,Age
1,John,20
2,"Jack, the second", 21Context
StackExchange Code Review Q#139208, answer score: 4
Revisions (0)
No revisions yet.