读取csv文件并将其存储到数组中? [英] Reading a csv file and storing it into an array?
本文介绍了读取csv文件并将其存储到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试读取以下文件并存储到数组中。首先,我需要拆分由空格分隔的字符串;还有更多我需要绘制它。有人可以帮帮我吗?
I am trying to read the following file and store into an array. First I need to split the strings separated by space and ; Further more i need to plot it. Can some one help me please?
150101 00:21:49,7 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 00:21:49,8 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 00:21:49,9 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:49,0 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,1 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,2 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,3 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,4 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,5 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,6 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,7 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,8 0030;0000;00;00;00;00;0000;0000;00;10;E008
我的尝试:
我试图读取文件并将其存储在一个数组中
What I have tried:
I have tried to read the file and store it in an array
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ComPlay
{
class Read
{
private string[] header;
private float[,] data;
private int nLines;
private int nColumns;
//constructor to be implemented
public Read()
{
}
//functions used for retrieving the data
public int get_nLines()
{
return nLines;
}
public int get_nColumns()
{
return nColumns;
}
public float[,] get_Data()
{
return data;
}
public string[] get_Header()
{
return header;
}
public Read(Stream myStream)
{
string aux;
string[] pieces;
//read the file line by line
StreamReader sr = new StreamReader(myStream);
aux = sr.ReadLine();
header = aux.Split(' ');
nColumns = header.Length;
nLines = 0;
while ((aux = sr.ReadLine()) != null)
{
if (aux.Length > 0) nLines++;
}
//read the numerical data from file in an array
data = new float[nLines, nColumns];
sr.BaseStream.Seek(0, 0);
sr.ReadLine();
for (int i = 0; i < nLines; i++)
{
aux = sr.ReadLine();
if(i<2)
{ pieces = aux.Split(' '); }
else
{ pieces = aux.Split(';');}
for (int j = 0; j < nColumns; j++)
data[i, j] = float.Parse(pieces[j]);
}
sr.Close();
}
}
}
推荐答案
你试过正则表达式吗?
了解它们是如何工作非常有用(痛苦的经历)。
Have you tried regular expressions?
It´s very useful to know how they work (painful experience).
var table = new List<string[]>();
using (var r = new StreamReader("filePathOrStreamHere"))
{
while (!r.EndOfStream)
{
string line = r.ReadLine();
table.Add(Regex.Split(line, @"\s|[;]|[,]"));
}
r.Close();
}
//table now contains a list of string arrays,
// where each array is a "row" of "cell" values.
当您以后使用数据时,可以将其转换为基于索引的数据类型?
When you are using the data later you can convert it into the datatype based on index?
As Sascha说,一个预先构建的CSV阅读器是一个更好的主意 - 他建议的那个也是我使用的那个:快速CSV阅读器 [ ^ ]
对于图表,一旦你读完数据就很容易了:使用具有来自集合的多行的图表 [ ^ ]应该有所帮助。
As Sascha says, a pre-built CSV reader is a much better idea - and the one he suggests is the one I use as well: A Fast CSV Reader[^]
For charting, it's pretty easy once you've read the data: Using a Chart With Multiple Lines From A Collection[^] should help.
为什么强行关门大开?
使用 Ado.net [ ^ ]。它是一组向.NET程序员公开数据访问服务的类。
请参阅:
ADO.NET(核心数据访问) [ ^ ]
读取文本文件(txt,csv,日志,制表符,固定长度) [ ^ ]
关于文本文件的大量ADO [ ^ ]
读取文本文件特定列 [ ^ ]
Why to force doors wide open?
Use Ado.net[^]. It is a set of classes that expose data access services to the .NET programmer.
See:
ADO.NET (core data access)[^]
Read Text File (txt, csv, log, tab, fixed length)[^]
Much ADO About Text Files[^]
Read Text File Specific Columns[^]
这篇关于读取csv文件并将其存储到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文