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 ...
随机推荐
- Linux Shell 之 Shell中的函数调用
说起函数调用,相信大家也不会陌生,然而对于初学Shell的我来说,Shell中函数调用方式却有点让我不太习惯,自己也走了不少的弯路,因为传递参数时出了一个很“自然”的错误,也让我吃了不少的苦头,所以总 ...
- 60s 经济学探奇
理解经济学 什么是经济学.对于学习金融的同学,一定会给你搬出一大堆定义.例证.学派.说经济学是一门研究研究价值的生产.流通.分配.消费的规律的理论. 非常高大上的感觉,可是对于我这样没有什么金融学理论 ...
- POJ 2991 Crane(线段树+计算几何)
POJ 2991 Crane 题目链接 题意:给定一个垂直的挖掘机臂.有n段,如今每次操作能够旋转一个位置,把[s, s + 1]专程a度,每次旋转后要输出第n个位置的坐标 思路:线段树.把每一段当成 ...
- HDOJ 4862 Jump
K路径覆盖问题,最小费用最大流.... ,费用0,Y部有N*M个节点,每一个节点向汇点连一条边,流量1,费用0,假设X部的节点x能够在一步之内到达Y部的节点y,那么就连边x->y,费用为从x格子 ...
- copy算法
copy------强化效率无所不用其极 copy(first,last,result)算法可将输入区间[first,last)内的元素拷贝到输出区间[result,result+(last-f ...
- 怎样处理iOS 5与iOS 6的 low-memory
移动设备终端的内存极为有限,应用程序必须做好low-memory处理工作,才能避免程序因内存使用过大而崩溃. low-memory 处理思路 通常一个应用程序会包含多个view controllers ...
- Android开源项目大全 - 工具类
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...
- FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区
FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区 FreeNAS 9.1.1 发布,网络存储系统
- 利用json获取天气信息
天气预报信息获取是利用json获取的,网上有非常多资源,源码.因为上面涉及到非常多天气信息,包含湿度,出行建议等,以及加入了全部城市代码的资源包.为了练手了解json的原理.我仅获取诚笃城市的最高温, ...
- 【Unity3D自学记录】可视化对照十多种排序算法(C#版)
在这篇文章中.我会向大家展示一些排序算法的可视化过程.我还写了一个工具.大家可对照查看某两种排序算法. 下载源代码 – 75.7 KB 下载演示样例 – 27.1 KB 引言 首先,我觉得是最重要的是 ...