示例#1
0
文件: ReadCsv.cs 项目: lanicon/netcsv
        /// <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>());
        }
示例#2
0
    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);
    }
示例#3
0
 /// <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;
 }
示例#4
0
 /// <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;
 }
示例#5
0
文件: ReadCsv.cs 项目: MaziarM/netcsv
        /// <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));
        }
示例#6
0
文件: ReadCsv.cs 项目: lanicon/netcsv
        /// <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));
        }
示例#7
0
文件: ReadCsv.cs 项目: MaziarM/netcsv
        /// <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));
        }