/// <summary> /// Read a file as CSV, using specific behaviour, layout and conversion options. /// </summary> /// <param name="path">The full or relative path name</param> /// <param name="encoding">The encoding of the file.</param> /// <param name="quote">The quote character. Default '"'</param> /// <param name="delimiter">Field delimiter. Default ','</param> /// <param name="escape">Quote escape character (for quotes inside fields). Default '\'</param> /// <param name="comment">Comment marker. Default '#'</param> /// <param name="hasHeaders">Is the first line a header line (default false)?</param> /// <param name="trimmingOptions">How should fields be trimmed?</param> /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param> /// <param name="skipEmptyLines">Should empty lines be skipped?</param> /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param> /// <param name="cultureInfo">Culture info to be used for parsing culture-sensitive data (such as date/time and decimal numbers)</param> /// <returns>a DataReader instance to read the contents of the CSV file</returns> public static IEnumerable <T> FromString <T>( string input, char quote = '"', char delimiter = ',', char escape = '"', char comment = '#', bool hasHeaders = false, ValueTrimmingOptions trimmingOptions = ValueTrimmingOptions.UnquotedOnly, MissingFieldAction missingFieldAction = MissingFieldAction.ParseError, bool skipEmptyLines = true, QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore, CultureInfo cultureInfo = null) { var schema = new CsvSchemaBuilder(cultureInfo).From <T>().Schema; return(FromString( input, quote, delimiter, escape, comment, hasHeaders, trimmingOptions, missingFieldAction, skipEmptyLines, quotesInsideQuotedFieldAction, schema, cultureInfo) .AsEnumerable <T>()); }
private static string[] Read(string data, char quote = '"', char delimiter = ',', char escape = '"', char comment = '#', bool hasHeaders = false, ValueTrimmingOptions trimmingOptions = ValueTrimmingOptions.None, MissingFieldAction missingFieldAction = MissingFieldAction.ParseError, QuotesInsideQuotedFieldAction quoteInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore, bool skipEmptyLines = false) { var reader = ReadCsv.FromString(data, quote, delimiter, escape, comment, hasHeaders, trimmingOptions, missingFieldAction, skipEmptyLines, quoteInsideQuotedFieldAction); reader.Read(); string[] results = new string[reader.FieldCount]; reader.GetValues(results); return(results); }
/// <summary> /// Constructs a CsvBehaviour instance that can be used to drive the csv parser /// </summary> /// <param name="trimmingOptions">How should fields be trimmed?</param> /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param> /// <param name="skipEmptyLines">Should empty lines be skipped?</param> /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param> public CsvBehaviour( ValueTrimmingOptions trimmingOptions = ValueTrimmingOptions.UnquotedOnly, MissingFieldAction missingFieldAction = MissingFieldAction.ParseError, bool skipEmptyLines = true, QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore) { _trimmingOptions = trimmingOptions; _missingFieldAction = missingFieldAction; _skipEmptyLines = skipEmptyLines; _quotesInsideQuotedFieldAction = quotesInsideQuotedFieldAction; }
/// <summary> /// Constructs a CsvBehaviour instance that can be used to drive the csv parser /// </summary> /// <param name="trimmingOptions">How should fields be trimmed?</param> /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param> /// <param name="skipEmptyLines">Should empty lines be skipped?</param> /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param> public CsvBehaviour( ValueTrimmingOptions trimmingOptions = ValueTrimmingOptions.UnquotedOnly, MissingFieldAction missingFieldAction = MissingFieldAction.ParseError, bool skipEmptyLines = true, QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore) { TrimmingOptions = trimmingOptions; MissingFieldAction = missingFieldAction; SkipEmptyLines = skipEmptyLines; QuotesInsideQuotedFieldAction = quotesInsideQuotedFieldAction; }
/// <summary> /// Read a string as CSV, using specific behaviour, layout and conversion options /// </summary> /// <param name="input">The CSV input</param> /// <param name="quote">The quote character. Default '"'</param> /// <param name="delimiter">Field delimiter. Default ','</param> /// <param name="escape">Quote escape character (for quotes inside fields). Default '\'</param> /// <param name="comment">Comment marker. Default '#'</param> /// <param name="hasHeaders">Is the first line a header line (default false)?</param> /// <param name="trimmingOptions">How should fields be trimmed?</param> /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param> /// <param name="skipEmptyLines">Should empty lines be skipped?</param> /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param> /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param> /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param> /// <returns>a datareader instance to read the contents of the CSV file</returns> public static IDataReader FromString( string input, char quote = '"', char delimiter = ',', char escape = '"', char comment = '#', bool hasHeaders = false, ValueTrimmingOptions trimmingOptions = ValueTrimmingOptions.UnquotedOnly, MissingFieldAction missingFieldAction = MissingFieldAction.ParseError, bool skipEmptyLines = true, QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore, IConverter converter = null, int bufferSize = 4096) { var reader = new StringReader(input); var layout = new CsvLayout(quote, delimiter, escape, comment, hasHeaders); var behaviour = new CsvBehaviour(trimmingOptions, missingFieldAction, skipEmptyLines, quotesInsideQuotedFieldAction); return(FromReader(reader, layout, behaviour, converter ?? Converter.Default, bufferSize)); }
/// <summary> /// Read a string as CSV, using specific behaviour, layout and conversion options /// </summary> /// <param name="input">The CSV input</param> /// <param name="quote">The quote character. Default '"'</param> /// <param name="delimiter">Field delimiter. Default ','</param> /// <param name="escape">Quote escape character (for quotes inside fields). Default '\'</param> /// <param name="comment">Comment marker. Default '#'</param> /// <param name="hasHeaders">Is the first line a header line (default false)?</param> /// <param name="trimmingOptions">How should fields be trimmed?</param> /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param> /// <param name="skipEmptyLines">Should empty lines be skipped?</param> /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param> /// <param name="schema">The CSV schema.</param> /// <param name="cultureInfo">Culture info to be used for parsing culture-sensitive data (such as date/time and decimal numbers)</param> /// <returns>a DataReader instance to read the contents of the CSV file</returns> public static IDataReader FromString( string input, char quote = '"', char delimiter = ',', char escape = '"', char comment = '#', bool hasHeaders = false, ValueTrimmingOptions trimmingOptions = ValueTrimmingOptions.None, MissingFieldAction missingFieldAction = MissingFieldAction.ParseError, bool skipEmptyLines = true, QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore, CsvSchema schema = null, CultureInfo cultureInfo = null) { var reader = new StringReader(input); var layout = new CsvLayout(quote, delimiter, escape, comment, hasHeaders, schema); var behaviour = new CsvBehaviour(trimmingOptions, missingFieldAction, skipEmptyLines, quotesInsideQuotedFieldAction); return(FromReader(reader, layout, behaviour, cultureInfo)); }
/// <summary> /// Read a file as CSV, using specific behaviour, layout and conversion options. Make sure to dispose the datareader. /// </summary> /// <param name="path">The full or relative path name</param> /// <param name="encoding">The encoding of the file. Default is UTF8.</param> /// <param name="quote">The quote character. Default '"'</param> /// <param name="delimiter">Field delimiter. Default ','</param> /// <param name="escape">Quote escape character (for quotes inside fields). Default '\'</param> /// <param name="comment">Comment marker. Default '#'</param> /// <param name="hasHeaders">Is the first line a header line (default false)?</param> /// <param name="trimmingOptions">How should fields be trimmed?</param> /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param> /// <param name="skipEmptyLines">Should empty lines be skipped?</param> /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param> /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader). When none is specified, System.Convert is used.</param> /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param> /// <returns>a datareader instance to read the contents of the CSV file</returns> public static IDataReader FromFile( string path, Encoding encoding = null, char quote = '"', char delimiter = ',', char escape = '"', char comment = '#', bool hasHeaders = false, ValueTrimmingOptions trimmingOptions = ValueTrimmingOptions.UnquotedOnly, MissingFieldAction missingFieldAction = MissingFieldAction.ParseError, bool skipEmptyLines = true, QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore, IConverter converter = null, int bufferSize = 4096) { // caller should dispose IDataReader, which will indirectly also close the stream var layout = new CsvLayout(quote, delimiter, escape, comment, hasHeaders); var behaviour = new CsvBehaviour(trimmingOptions, missingFieldAction, skipEmptyLines, quotesInsideQuotedFieldAction); var stream = File.OpenRead(path); var reader = new StreamReader(stream, encoding ?? Encoding.UTF8); return(FromReader(reader, layout, behaviour, converter ?? Converter.Default, bufferSize)); }