/// <summary> /// Initializes a new instance of the CsvReader class. /// </summary> /// <param name="reader">A <see cref="T:TextReader" /> pointing to the CSV file.</param> /// <param name="hasHeaders"> /// <see langword="true" /> if field names are located on the first non commented line, otherwise, /// <see langword="false" />. /// </param> /// <param name="delimiter">The delimiter character separating each field (default is ',').</param> /// <param name="quote">The quotation character wrapping every field (default is ''').</param> /// <param name="escape"> /// The escape character letting insert quotation characters inside a quoted field (default is '\'). /// If no escape character, set to '\0' to gain some performance. /// </param> /// <param name="comment">The comment character indicating that a line is commented out (default is '#').</param> /// <param name="trimSpaces"> /// <see langword="true" /> if spaces at the start and end of a field are trimmed, otherwise, /// <see langword="false" />. Default is <see langword="true" />. /// </param> /// <param name="bufferSize">The buffer size in bytes.</param> /// <exception cref="T:ArgumentNullException"> /// <paramref name="reader" /> is a <see langword="null" />. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bufferSize" /> must be 1 or more. /// </exception> public CachedCsvReader(TextReader reader, bool hasHeaders, char delimiter, char quote, char escape, char comment, ValueTrimmingOptions trimmingOptions, int bufferSize) : base(reader, hasHeaders, delimiter, quote, escape, comment, trimmingOptions, bufferSize) { _records = new List <string[]>(); _currentRecordIndex = -1; }
/// <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; }
public void TrimFieldValuesTest(string data, ValueTrimmingOptions trimmingOptions, params string[] expected) { using var csv = ReadCsv.FromString(data, trimmingOptions: trimmingOptions); while (csv.Read()) { var actual = new string[csv.FieldCount]; csv.GetValues(actual); CollectionAssert.AreEqual(expected, actual); } }
/// <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; }
public void TrimFieldValuesTest(string data, ValueTrimmingOptions trimmingOptions, params string[] expected) { using (var csv = new CsvRecordReader(new StringReader(data), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, trimmingOptions)) { foreach (CsvRecord record in csv) { var actual = record.ToArray(); CollectionAssert.AreEqual(expected, actual); } } }
public void TrimFieldValuesTest(string data, ValueTrimmingOptions trimmingOptions, params string[] expected) { using (var csv = new CsvReader(new StringReader(data), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, trimmingOptions)) { while (csv.ReadNextRecord()) { var actual = new string[csv.FieldCount]; csv.CopyCurrentRecordTo(actual); Assert.AreEqual(expected, actual); } } }
private T ParseExceptionTrimmingOptions <T>(string data, ValueTrimmingOptions options = ValueTrimmingOptions.UnquotedOnly) where T : Exception { var ep = Assert.Throws <T>(() => { using (var csv = new CsvReader(new StringReader(data), false, ',', '"', '\\', '#', options)) { while (csv.ReadNextRecord()) { for (var i = 0; i < csv.FieldCount; i++) { var s = csv[i]; } } } }); return(ep); }
/// <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)); }
/// <summary> /// Initializes a new instance of the CsvReader class. /// </summary> /// <param name="reader">A <see cref="T:TextReader"/> pointing to the CSV file.</param> /// <param name="hasHeaders"><see langword="true"/> if field names are located on the first non commented line, otherwise, <see langword="false"/>.</param> /// <param name="delimiter">The delimiter character separating each field (default is ',').</param> /// <param name="quote">The quotation character wrapping every field (default is ''').</param> /// <param name="escape"> /// The escape character letting insert quotation characters inside a quoted field (default is '\'). /// If no escape character, set to '\0' to gain some performance. /// </param> /// <param name="comment">The comment character indicating that a line is commented out (default is '#').</param> /// <param name="trimSpaces"><see langword="true"/> if spaces at the start and end of a field are trimmed, otherwise, <see langword="false"/>. Default is <see langword="true"/>.</param> /// <param name="bufferSize">The buffer size in bytes.</param> /// <exception cref="T:ArgumentNullException"> /// <paramref name="reader"/> is a <see langword="null"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bufferSize"/> must be 1 or more. /// </exception> public CachedCsvReader(TextReader reader, bool hasHeaders, char delimiter, char quote, char escape, char comment, ValueTrimmingOptions trimmingOptions, int bufferSize) : base(reader, hasHeaders, delimiter, quote, escape, comment, trimmingOptions, bufferSize) { _records = new List<string[]>(); _currentRecordIndex = -1; }
/// <summary> /// Initializes a new instance of the CsvReader class. /// </summary> /// <param name="reader">A <see cref="T:TextReader"/> pointing to the CSV file.</param> /// <param name="hasHeaders"><see langword="true"/> if field names are located on the first non commented line, otherwise, <see langword="false"/>.</param> /// <param name="delimiter">The delimiter character separating each field (default is ',').</param> /// <param name="quote">The quotation character wrapping every field (default is ''').</param> /// <param name="escape"> /// The escape character letting insert quotation characters inside a quoted field (default is '\'). /// If no escape character, set to '\0' to gain some performance. /// </param> /// <param name="comment">The comment character indicating that a line is commented out (default is '#').</param> /// <param name="trimmingOptions">Determines how values should be trimmed.</param> /// <exception cref="T:ArgumentNullException"> /// <paramref name="reader"/> is a <see langword="null"/>. /// </exception> /// <exception cref="T:ArgumentException"> /// Cannot read from <paramref name="reader"/>. /// </exception> public CachedCsvReader(TextReader reader, bool hasHeaders, char delimiter, char quote, char escape, char comment, ValueTrimmingOptions trimmingOptions) : this(reader, hasHeaders, delimiter, quote, escape, comment, trimmingOptions, DefaultBufferSize) { }
/// <summary> /// Initializes a new instance of the CsvRecordReader class. /// </summary> /// <param name="reader">A <see cref="T:TextReader"/> pointing to the CSV file.</param> /// <param name="hasHeaders"><see langword="true"/> if field names are located on the first non commented line, otherwise, <see langword="false"/>.</param> /// <param name="delimiter">The delimiter character separating each field (default is ',').</param> /// <param name="quote">The quotation character wrapping every field (default is ''').</param> /// <param name="escape"> /// The escape character letting insert quotation characters inside a quoted field (default is '\'). /// If no escape character, set to '\0' to gain some performance. /// </param> /// <param name="comment">The comment character indicating that a line is commented out (default is '#').</param> /// <param name="trimmingOptions">Determines which values should be trimmed.</param> /// <param name="bufferSize">The buffer size in bytes.</param> /// <exception cref="T:ArgumentNullException"> /// <paramref name="reader"/> is a <see langword="null"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bufferSize"/> must be 1 or more. /// </exception> public CsvRecordReader(TextReader reader, bool hasHeaders, char delimiter, char quote, char escape, char comment, ValueTrimmingOptions trimmingOptions, int bufferSize) { this.csvReader = new CsvReader(reader, hasHeaders, delimiter, quote, escape, comment, trimmingOptions, bufferSize); if (hasHeaders) { var headersArray = csvReader.GetFieldHeaders(); this.headers = new CsvHeaders(headersArray); } }
/// <summary> /// Initializes a new instance of the CsvRecordReader class. /// </summary> /// <param name="reader">A <see cref="T:TextReader"/> pointing to the CSV file.</param> /// <param name="hasHeaders"><see langword="true"/> if field names are located on the first non commented line, otherwise, <see langword="false"/>.</param> /// <param name="delimiter">The delimiter character separating each field (default is ',').</param> /// <param name="quote">The quotation character wrapping every field (default is ''').</param> /// <param name="escape"> /// The escape character letting insert quotation characters inside a quoted field (default is '\'). /// If no escape character, set to '\0' to gain some performance. /// </param> /// <param name="comment">The comment character indicating that a line is commented out (default is '#').</param> /// <param name="trimmingOptions">Determines which values should be trimmed.</param> /// <exception cref="T:ArgumentNullException"> /// <paramref name="reader"/> is a <see langword="null"/>. /// </exception> /// <exception cref="T:ArgumentException"> /// Cannot read from <paramref name="reader"/>. /// </exception> public CsvRecordReader(TextReader reader, bool hasHeaders, char delimiter, char quote, char escape, char comment, ValueTrimmingOptions trimmingOptions) : this(reader, hasHeaders, delimiter, quote, escape, comment, trimmingOptions, CsvReader.DefaultBufferSize) { }
/// <summary> /// Initializes a new instance of the CsvReader class. /// </summary> /// <param name="reader">A <see cref="T:TextReader"/> pointing to the CSV file.</param> /// <param name="hasHeaders"><see langword="true"/> if field names are located on the first non commented line, otherwise, <see langword="false"/>.</param> /// <param name="delimiter">The delimiter character separating each field (default is ',').</param> /// <param name="quote">The quotation character wrapping every field (default is ''').</param> /// <param name="escape"> /// The escape character letting insert quotation characters inside a quoted field (default is '\'). /// If no escape character, set to '\0' to gain some performance. /// </param> /// <param name="comment">The comment character indicating that a line is commented out (default is '#').</param> /// <param name="trimmingOptions">Determines how values should be trimmed.</param> /// <param name="nullValue">The value which denotes a DbNull-value.</param> /// <exception cref="T:ArgumentNullException"> /// <paramref name="reader"/> is a <see langword="null"/>. /// </exception> /// <exception cref="T:ArgumentException"> /// Cannot read from <paramref name="reader"/>. /// </exception> public CachedCsvReader(TextReader reader, bool hasHeaders, char delimiter, char quote, char escape, char comment, ValueTrimmingOptions trimmingOptions, string nullValue = null) : this(reader, hasHeaders, delimiter, quote, escape, comment, trimmingOptions, DefaultBufferSize, nullValue) { }
public void TrimFieldValuesTest(string data, ValueTrimmingOptions trimmingOptions, params string[] expected) { using (var csv = new CsvReader(new StringReader(data), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, trimmingOptions)) { while (csv.ReadNextRecord()) { var actual = new string[csv.FieldCount]; csv.CopyCurrentRecordTo(actual); Assert.AreEqual(expected, actual); } } }
internal bool ShouldTrim(bool quoted, ValueTrimmingOptions trimmingOptions) => trimmingOptions switch {
/// <summary> /// Initializes a new instance of the CsvReader class. /// </summary> /// <param name="reader">A <see cref="TextReader"/> pointing to the CSV file.</param> /// <param name="hasHeaders"><see langword="true"/> if field names are located on the first non commented line, otherwise, <see langword="false"/>.</param> /// <param name="delimiter">The delimiter character separating each field (default is ',').</param> /// <param name="quote">The quotation character wrapping every field (default is ''').</param> /// <param name="escape"> /// The escape character letting insert quotation characters inside a quoted field (default is '\'). /// If no escape character, set to '\0' to gain some performance. /// </param> /// <param name="comment">The comment character indicating that a line is commented out (default is '#').</param> /// <param name="trimmingOptions">Determines which values should be trimmed.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="reader"/> is a <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// Cannot read from <paramref name="reader"/>. /// </exception> public CsvReader(TextReader reader, bool hasHeaders, char delimiter, char quote, char escape, char comment, ValueTrimmingOptions trimmingOptions) : this(reader, DefaultBufferSize, new CsvLayout(hasHeaders : hasHeaders, delimiter : delimiter, quote : quote, escape : escape, comment : comment), new CsvBehaviour(trimmingOptions : trimmingOptions)) { }
public void TrimFieldValuesTest(string data, ValueTrimmingOptions trimmingOptions, params string[] expected) { using (var csv = new CsvRecordReader(new StringReader(data), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, trimmingOptions)) { foreach (CsvRecord record in csv) { var actual = record.ToArray(); CollectionAssert.AreEqual(expected, actual); } } }