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 ...
随机推荐
- hdu 4055 Number String(有点思维的DP)
Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Swift调用Objective-C编写的代码(颜色选择器KKColorListPicker调用)
在Swift项目中,我们可以导入任意用Objective-C写的框架,代码库等.下面以Swift调用Objective-C编写的颜色选择器KKColorListPicker为例. 效果图如下: ...
- C#数学运算表达式解释器
C#数学运算表达式解释器 測试文件内容: a=2+3*2; b=2*(2+3); 浏览按钮事件处理程序: private void button_browse_Click(object sender, ...
- C++历史
C++历史 早期C++ •1979: 首次实现引入类的C(C with Classes first implemented) 1.新特性:类.成员函数.继承类.独立编译.公共和私有访问控制.友元.函数 ...
- TCP连接的建立(二)
被动打开 SYN cookies TCP协议开辟了一个比較大的内存空间请求连接队列来存储连接请求块,当SYN请求不断添加,请求连接数目到达上限时,会致使系统丢弃SYN连接请求.SYN cookies技 ...
- [每日一题] 11gOCP 1z0-052 :2013-09-5 runInstaller oracle of no swap
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11186995 正确答案:A 我们先来看下面这张截图,这是我在安装Oracle 11.2.0 ...
- [Cocos2d-x]博客推荐
推荐一下大神们的博客: JackyStudio: http://blog.csdn.net/jackyvincefu/article/category/1591201/3 老G的小屋: http:// ...
- oracle事务(转)
今天温习oracle事务,记录如下: 事务定义 事务是保持数据的一致性,它由相关的DDL或者DML语句做为载体,这组语句执行的结果要么一起成功,要么一起失败. 我们 ...
- OpenStack25
OpenStack(25) API 前端服务 每个 OpenStack 组件可能包含若干子服务,其中必定有一个 API 服务负责接收客户请求. 以 Nova 为例,nova-api 作为 Nova 组 ...
- python语言学习9——使用list和tuple
list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 位置 用索引来访问list中每一个位置的元素,记得索引是从0开始的,到 len-1结 ...