C#CSV文件到数组/列表
我想在C中读取一些数组中的4-5个CSV文件# 我知道有人问过我这个问题,我也经历过这些问题。。。 但是我对CSV的使用太简单了 我有以下数据类型列的csv文件C#CSV文件到数组/列表,c#,.net,csv,csv-import,C#,.net,Csv,Csv Import,我想在C中读取一些数组中的4-5个CSV文件# 我知道有人问过我这个问题,我也经历过这些问题。。。 但是我对CSV的使用太简单了 我有以下数据类型列的csv文件 string,string 这些弦没有“,”所以没有张力。。。 就这样。而且它们也不大。每张唱片只有20张左右 我只想把它们读入C的数组 有什么非常简单直接的方法可以做到这一点吗?要读取文件,请使用 TextReader reader = File.OpenText(filename); 读一行: string line = read
string,string
这些弦没有“,”所以没有张力。。。
就这样。而且它们也不大。每张唱片只有20张左右
我只想把它们读入C的数组
有什么非常简单直接的方法可以做到这一点吗?要读取文件,请使用
TextReader reader = File.OpenText(filename);
读一行:
string line = reader.ReadLine()
然后
把他们分开
通过在最后两个示例行周围使用循环,您可以将每个令牌数组添加到一个列表中(如果您需要的话)。此循环包括字段中的引号和逗号。(假设您一次只做一行)
下面是一种将CSV内容获取到字符串数组的简单方法。CSV文件可以有双引号、回车换行符和逗号分隔符。 以下是您需要的库:
System.IO代码>
System.Collection.Generic代码>
System.IO
用于FileStream
和StreamReader
类访问您的文件。这两个类都实现了IDisposable
接口,因此您可以使用using
语句来关闭流。(下面的例子)
System.Collection.Generic
命名空间用于集合,例如IList
、List
和ArrayList
等。。。在本例中,我们将使用List
类,因为在我看来,列表比数组好。但是,在返回出站变量之前,我将调用.ToArray()
成员方法来返回数组
有很多方法可以从文件中获取内容,我个人更喜欢使用while(condition)
循环来迭代内容。在条件
子句中,使用!lReader.EndOfStream
。当不是流的结尾时,继续在文件上迭代
public string[] GetCsvContent(string iFileName)
{
List<string> oCsvContent = new List<string>();
using (FileStream lFileStream =
new FileStream(iFilename, FileMode.Open, FileAccess.Read))
{
StringBuilder lFileContent = new StringBuilder();
using (StreamReader lReader = new StreamReader(lFileStream))
{
// flag if a double quote is found
bool lContainsDoubleQuotes = false;
// a string for the csv value
string lCsvValue = "";
// loop through the file until you read the end
while (!lReader.EndOfStream)
{
// stores each line in a variable
string lCsvLine = lReader.ReadLine();
// for each character in the line...
foreach (char lLetter in lCsvLine)
{
// check if the character is a double quote
if (lLetter == '"')
{
if (!lContainsDoubleQuotes)
{
lContainsDoubleQuotes = true;
}
else
{
lContainsDoubleQuotes = false;
}
}
// if we come across a comma
// AND it's not within a double quote..
if (lLetter == ',' && !lContainsDoubleQuotes)
{
// add our string to the array
oCsvContent.Add(lCsvValue);
// null out our string
lCsvValue = "";
}
else
{
// add the character to our string
lCsvValue += lLetter;
}
}
}
}
}
return oCsvContent.ToArray();
}
public string[]GetCsvContent(string-iFileName)
{
List oCsvContent=新列表();
使用(FileStream lFileStream=
新文件流(iFilename、FileMode.Open、FileAccess.Read))
{
StringBuilder lFileContent=新建StringBuilder();
使用(StreamReader lReader=新StreamReader(lFileStream))
{
//如果找到双引号,则标记
bool lContainsDoubleQuotes=假;
//csv值的字符串
字符串lCsvValue=“”;
//循环浏览文件,直到读到结尾
而(!lReader.EndOfStream)
{
//将每一行存储在一个变量中
字符串lCsvLine=lReader.ReadLine();
//对于行中的每个字符。。。
foreach(lCsvLine中的字符)
{
//检查字符是否为双引号
如果(lLetter==“”)
{
如果(!lContainsDoubleQuotes)
{
lContainsDoubleQuotes=true;
}
其他的
{
lContainsDoubleQuotes=false;
}
}
//如果我们遇到逗号
//而且它不在双引号内。。
如果(lLetter==','&&!lContainsDoubleQuotes)
{
//将字符串添加到数组中
oCsvContent.Add(lCsvValue);
//清空我们的字符串
lCsvValue=“”;
}
其他的
{
//将字符添加到字符串中
lCsvValue+=lLetter;
}
}
}
}
}
返回oCsvContent.ToArray();
}
希望这有帮助!非常简单和快速。
干杯!看看那边的相关问题-->什么是“C#数组”?@GrantThomas:一把眼镜?(可能是一部关于亡命之徒面对年老和视力低下的蹂躏的新电影)如果列条目的字符串值中包含逗号,该怎么办?如果CSV文件是逗号分隔的,则标记中的逗号是非法的,或者无法正确解析。分隔字符显然应该是标记中未使用的字符。如果主题开始时在其单元格中使用逗号,我会假定不会导出到逗号分隔的列表!这完全是错误的,作为一名程序员,我希望人们知道一些转义值。给定一个只包含以下内容的CSV文件:a、b、c、d、e、f、g,您知道它们来自2个单元格。您如何知道哪些字母来自哪些单元格?a、'a、b、c、d、e、f、g'
…或者可能有b这是一个正在进行的自定义闹剧,a,a\,b\,c\,d\,e\,f\,g
。无可否认,这很奇怪,但出于这个目的,让我们假设它超出了处理输入显示方式的范围,只需要正确解析,使用斜杠和所有字符……但仍然只使用该字符串作为单个值。
using Microsoft.VisualBasic.FileIO; //For TextFieldParser
// blah blah blah
StringReader csv_reader = new StringReader(csv_line);
TextFieldParser csv_parser = new TextFieldParser(csv_reader);
csv_parser.SetDelimiters(",");
csv_parser.HasFieldsEnclosedInQuotes = true;
string[] csv_array = csv_parser.ReadFields();
public string[] GetCsvContent(string iFileName)
{
List<string> oCsvContent = new List<string>();
using (FileStream lFileStream =
new FileStream(iFilename, FileMode.Open, FileAccess.Read))
{
StringBuilder lFileContent = new StringBuilder();
using (StreamReader lReader = new StreamReader(lFileStream))
{
// flag if a double quote is found
bool lContainsDoubleQuotes = false;
// a string for the csv value
string lCsvValue = "";
// loop through the file until you read the end
while (!lReader.EndOfStream)
{
// stores each line in a variable
string lCsvLine = lReader.ReadLine();
// for each character in the line...
foreach (char lLetter in lCsvLine)
{
// check if the character is a double quote
if (lLetter == '"')
{
if (!lContainsDoubleQuotes)
{
lContainsDoubleQuotes = true;
}
else
{
lContainsDoubleQuotes = false;
}
}
// if we come across a comma
// AND it's not within a double quote..
if (lLetter == ',' && !lContainsDoubleQuotes)
{
// add our string to the array
oCsvContent.Add(lCsvValue);
// null out our string
lCsvValue = "";
}
else
{
// add the character to our string
lCsvValue += lLetter;
}
}
}
}
}
return oCsvContent.ToArray();
}