C# 将csv文件中的两行添加到阵列
我有一个包含以下数据的csv文件: 500000,0.0056000 690000,0.0035200 我需要将每一行添加为一个单独的数组。所以50000,0.005,6000将是1。我该怎么做 目前,我的代码将每列添加到一个元素中 例如,数据[0]显示500000 690000C# 将csv文件中的两行添加到阵列,c#,C#,我有一个包含以下数据的csv文件: 500000,0.0056000 690000,0.0035200 我需要将每一行添加为一个单独的数组。所以50000,0.005,6000将是1。我该怎么做 目前,我的代码将每列添加到一个元素中 例如,数据[0]显示500000 690000 static void ReadFromFile(string filePath) { try { // Create an instance of St
static void ReadFromFile(string filePath)
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filePath))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(',');
Console.WriteLine(data[0] + " " + data[1]);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
使用您提供的有限数据集
const string test = @"500000,0.005,6000
690000,0.003,5200";
var result = test.Split('\n')
.Select(x=> x.Split(',')
.Select(y => Convert.ToDecimal(y))
.ToArray()
)
.ToArray();
foreach (var element in result)
{
Console.WriteLine($"{element[0]}, {element[1]}, {element[2]}");
}
没有林克能做到吗?是的,但是很乱
const string test = @"500000,0.005,6000
690000,0.003,5200";
List<decimal[]> resultList = new List<decimal[]>();
string[] lines = test.Split('\n');
foreach (var line in lines)
{
List<decimal> decimalValueList = new List<decimal>();
string[] splitValuesByComma = line.Split(',');
foreach (string value in splitValuesByComma)
{
decimal convertedValue = Convert.ToDecimal(value);
decimalValueList.Add(convertedValue);
}
decimal[] decimalValueArray = decimalValueList.ToArray();
resultList.Add(decimalValueArray);
}
decimal[][] resultArray = resultList.ToArray();
const字符串测试=@“500000,0.0056000
690000,0.003,5200";
列表结果列表=新列表();
字符串[]行=test.Split('\n');
foreach(行中的var行)
{
List decimalValueList=新列表();
string[]splitValuesByComma=line.Split(',');
foreach(splitValuesByComma中的字符串值)
{
十进制转换值=转换为十进制(值);
小数点列表。添加(转换值);
}
decimal[]decimalValueArray=decimalValueList.ToArray();
结果列表.Add(小数值数组);
}
十进制[][]resultArray=resultList.ToArray();
如果您可以使用
列表
,就不必担心数组长度,那么这将给出与我对第一个示例所做的完全相同的输出。
在以下示例中,变量line
将是一个列表数组,如下所示:
[“500000”、“0.005”、“6000”]
[“690000”、“0.003”、“5200”]
静态void ReadFromFile(字符串文件路径)
{
尝试
{
//创建StreamReader的实例以从文件中读取。
//using语句还将关闭StreamReader。
使用(StreamReader sr=新的StreamReader(文件路径))
{
列表行=新列表();
弦线;
//读取并显示文件中的行,直到
//已到达该文件。
而((line=sr.ReadLine())!=null)
{
string[]splittedLine=line.Split(',');
行。添加(拆分行);
}
}
}
捕获(例外e)
{
//让用户知道出了什么问题。
WriteLine(“无法读取文件:”);
控制台写入线(e.Message);
}
}
当其他方法有拆分方法时,我将有一个更“语常会”-“指定”的方法。
文件中有一些Csv值。查找存储在Csv中的此对象的名称,命名每列,然后键入它们。
定义这些字段的默认值。定义缺少的列和格式错误的字段的情况。头球
现在你知道你拥有什么,定义你想要什么。这一次:对象名称->属性->类型
信不信由你,输入和输出的简单定义解决了你的问题。
用于简化代码
CSV文件定义:
public class CsvItem_WithARealName
{
public int data1;
public decimal data2;
public int goodVariableNames;
}
public class CsvItemMapper : ClassMap<CsvItem_WithARealName>
{
public CsvItemMapper()
{ //mapping based on index. cause file has no header.
Map(m => m.data1).Index(0);
Map(m => m.data2).Index(1);
Map(m => m.goodVariableNames).Index(2);
}
}
您还没有向我们展示如何填充阵列。我不太清楚,您想要一个2d阵列吗?或者项目用空格分隔的1d数组?其中是抵押数组,因为数据数组刚刚好抱歉,这是一个输入错误,它的意思是数据[0]没有
string[]]results=File.ReadAllLines(filePath)。选择(x=>x.Split(',')).ToArray()代码>为您工作?在此处创建或初始化数组的位置是?有没有办法不用System.Linq;?另外,将两行添加到同一数组中似乎也是一个问题……将两行添加到同一数组是什么意思?每行都在其自己的数组中,因此行1
在数组1
中,行2
在数组2
中,这些数组然后存储在另一个用作容器的数组中。因此父数组具有值[array1,array2]
,array1
包含值[500000,0.0056000]
。而array2
包含[690000,0.003520]
。。。所以本质上你拥有的是[[500000,0.0056000],[690000,0.0035200]
这里没有包含[500000,0.0056000690000,0.0035200]
@Aydinand我想他是说“父”数组是“相同的”,但这正是OP想要的,所以,幸运的是,我不能使用列表。@praiseodin-在你的问题中,它在哪里这么说?
public class CsvItem_WithARealName
{
public int data1;
public decimal data2;
public int goodVariableNames;
}
public class CsvItemMapper : ClassMap<CsvItem_WithARealName>
{
public CsvItemMapper()
{ //mapping based on index. cause file has no header.
Map(m => m.data1).Index(0);
Map(m => m.data2).Index(1);
Map(m => m.goodVariableNames).Index(2);
}
}
private IEnumerable<CsvItem_WithARealName> GetCsvItems(string filePath)
{
using (var fileReader = File.OpenText(filePath))
using (var csvReader = new CsvHelper.CsvReader(fileReader))
{
csvReader.Configuration.CultureInfo = CultureInfo.InvariantCulture;
csvReader.Configuration.HasHeaderRecord = false;
csvReader.Configuration.RegisterClassMap<CsvItemMapper>();
while (csvReader.Read())
{
var record = csvReader.GetRecord<CsvItem_WithARealName>();
yield return record;
}
}
}
var filename = "csvExemple.txt";
var items = GetCsvItems(filename);