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的更多相关文章
- Qt Read and Write Csv File
This page discusses various available options for working with csv documents in your Qt application. ...
- Drag & Drop and File Reader
参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/det ...
- ogr2ogr: Export Well Known Text (WKT) for one feature to a CSV file
Perhaps you’re looking for this? ogr2ogr -f “CSV” “E:\4_GIS\NorthArkCartoData\UnitedStates\MO_wkt” “ ...
- SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values. Create TestTabl ...
- [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, ...
- python之模块csv之 读取CSV文件(reader和DictReader2个方法)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #读取CSV文件(reader和DictReader2个方法) import csv #csv文件,是一种常用 ...
- 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 ...
- Python: Write UTF-8 characters to csv file
To use codecs, we can write UTF-8 characters into csv file import codecs with open('ExcelUtf8.csv', ...
- save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv)
save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv) 2019-10-2 ...
随机推荐
- SQLite中如何用api操作BLOB类型的字段
在实际的编程开发当中我们经常要处理一些大容量二进制数据的存储,如图片或者音乐等等.对于这些二进制数据(blob字段)我们不能像处理普通的文本那样简单的插入或者查询,为此SQLite提供了一组函数来处理 ...
- Android中特殊图形的生成样例
import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; impor ...
- android 在你的UI中显示Bitmap - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Displaying Bitmaps in Your UI 在你的UI中显示Bitmap ...
- 警惕!iPhone 6即将上市 诈骗邮件已现身网络
随着iPhone 6即将上市,各路小道消息已经開始满天飞.就在近几日,一些记者还收到了假的iPhone 6将要上市的通知邮件.趋势科技也收到了几封,下面是样本之中的一个: (垃圾邮件样本) 不清楚iP ...
- 【web必知必会】—— 图解HTTP(转)good
本篇总结关于http的相关知识,主要内容参考如下导图: 主要讲解的内容有: 1 URL与URI的区别. 2 请求报文与相应报文的内容. 3 GET与POST的区别. 4 http的cookie.持久化 ...
- 在html中写python代码的语法和特点-----基于webpy的httpserver
在html文件里写python语法的内容,的注意事项: 1:python程序中的变量通过以下方法传入到html: 1:通过全局变量 :全局变量是不须要用$def with语法实现传递的,仅仅要定义了 ...
- Nginx特点
Nginx特点:1,跨平台:Nginx 能够在大多数 Unix like OS编译执行,并且也有Windows的移植版本号.2,配置异常简单:很easy上手.配置风格跟程序开发一样,神一般的配置.3, ...
- 基于Hadoop的地震数据分析统计
源码下载地址:http://download.csdn.net/detail/huhui_bj/5645641 opencsv下载地址:http://download.csdn.net/detail/ ...
- MVC之Session State性能
ASP.NET MVC之Session State性能问题(七) 前言 这一节翻译一篇有关Session State性能问题的文章,非一字一句翻译. 话题 不知道我们在真实环境中是否用到了Sess ...
- Beijing Perl Workshop - Augest 10th, 2013
Beijing Perl Workshop - Augest 10th, 2013 Beijing Perl Workshop