C# - CSV file reader

// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   Defines the CSVFileReader type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CSVFileReader
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Reads a CSV file.
    /// </summary>
    public class CSVFileReader
    {
        /// <summary>
        /// The stream reader to process the CSV file reading.
        /// </summary>
        private StreamReader streamReader;

        /// <summary>
        /// Initializes a new instance of the <see cref="CSVFileReader"/> class.
        /// </summary>
        /// <param name="csvFilePath">
        /// The CSV file path.
        /// </param>
        /// <param name="delimiter">
        /// The delimiter.
        /// </param>
        public CSVFileReader(string csvFilePath, char delimiter)
        {
            this.CSVFilePath = csvFilePath;
            this.Delimiter = delimiter;
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="CSVFileReader"/> class.
        /// </summary>
        ~CSVFileReader()
        {
            this.Close();
        }

        /// <summary>
        /// Gets the CSV file path being read.
        /// </summary>
        public string CSVFilePath { get; private set; }

        /// <summary>
        /// Gets the delimiter used within the CSV file.
        /// </summary>
        public char Delimiter { get; private set; }

        /// <summary>
        /// Read a line from the CSV file into a list of strings.
        /// </summary>
        /// <returns>
        /// The list of string.
        /// </returns>
        public List<string> ReadLine()
        {
            this.Open();
            var resultElements = new List<string>();
            try
            {
                var currentLine = this.streamReader.ReadLine();
                if (currentLine != null)
                {
                    var currentLineElements = currentLine.Split(this.Delimiter);
                    resultElements.AddRange(currentLineElements);
                }
            }
            catch (Exception)
            {
                this.Close();
            }

            return resultElements;
        }

        /// <summary>
        /// Opens the stream reader.
        /// </summary>
        private void Open()
        {
            if (this.streamReader == null)
            {
                this.streamReader = new StreamReader(this.CSVFilePath, Encoding.GetEncoding(1252));
            }
        }

        /// <summary>
        /// Close the stream reader.
        /// </summary>
        private void Close()
        {
            if (this.streamReader == null)
            {
                return;
            }

            this.streamReader.Close();
            this.streamReader.Dispose();
        }
    }
}
// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CSVFileReader
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The program.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main method.
        /// </summary>
        public static void Main()
        {
            var foo = new CSVFileReader(@"C:\Users\Administrator\Desktop\Tmp.csv", ',');
            List<string> line;
            while ((line = foo.ReadLine()).Count != 0)
            {
                foreach (var item in line)
                {
                    Console.Write(item + "|");
                }

                Console.WriteLine(string.Empty);
            }
        }
    }
}

C# - CSV file reader,布布扣,bubuko.com

时间: 2024-08-29 08:27:40

C# - CSV file reader的相关文章

python csv 模块reader后转换为列表

fh = open("mylist_wincsv.csv", 'rt') reader = csv.reader(fh) data = list(reader) print "Data cells from CSV:" print data[0][1], data[1][1] print data[0][2], data[1][2] print data[0][3], data[1][3] 以上是书上的代码.可是无法实现.len(list(reader)) =0 查

How to handle csv file using python

As i need to read something from a csv file using python.  I got something and put it here. Module: csv import csv FILE_FULL_PATH = 'D:\\Work\\script\\My Script\\Book1.csv' def f(): with open(FILE_FULL_PATH,'rb') as csvfile: for row in csv.reader(csv

Drag &amp; Drop and File Reader

参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/details/25295899 http://www.tutorialspoint.com/html5/html5_drag_drop.htm 本篇只作为个人参考 FIle Reader method abort()  停止读 readAsText(file,"utf-8") , 第2参数是指

a helper class for generating xls or csv file

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Office.Interop.Excel; namespace Reuters.ProcessQuality.ContentAuto.Lib{ public class XlsOrCsvUtil { /// <summary> /// Generate Xls Or Csv File /// <

解决Jmeter Non GUI运行时报“...ensure the jmeter .save.saveservice.* properties are the same as when the CSV file was created or the file may be read incorrectly”错误的问题

错误信息: File 'xxx.jtl' does not contain the field names header, ensure the jmeter.save.saveservice.* properties are the same as when the CSV file was created orthe file may be read incorrectly 解决: jmeter.properties配置文件 jmeter.save.saveservice.output_fo

[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

Hello everyone, this is the third post of the series. .   Background =============== In my solution, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Withou

Powercli随笔 - PowerCLI script to sequentially Storage vMotion VMs from a CSV File

PowerCLI script to sequentially Storage vMotion VMs from a CSV File This is a PowerCLI script that I use to Storage vMotion (s/vmotion) VMs from an input file (CSV File). This helps me evacuate VMs from a datastore that will be decommissioned and thi

Extending JMeter – Creating Custom Config Element – Property File Reader

JMeter is one of the best open source tools in the Test Automation Community. It comes with all the possible extensions to come up with our test scripts quickly. To make our life even more easier, It also lets us to come up with our own plugins by im

Csharp--Read Csv file to DataTable

在网上找的资料都不怎么好使,许多代码一看就知道根本没有考虑全面. 最后找到一个好用的,在codeplex上,这位老兄写成了一个framework,太重了. http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader 确实挺好用的. 我没耐下性子看他的实现,自己尝试写了如下的代码来完成了阅读csv. 参照:http://msdn.microsoft.com/en-us/library/ae5bf541%28v=vs.90%29.aspx 只