简体   繁体   English

C# - 导入txt多行逗号分隔文件并将其转换为二维数组

[英]C# - import txt multi-line comma delimited file and convert it to 2-dimensional array

I'm quite new to C# so excuse me for the newbie Q...我对 C# 很陌生,所以请原谅我的新手 Q...

I have a comma-delimited file with about 500 lines that looks like this: http://pastebin.com/JZyswTNX我有一个大约 500 行的逗号分隔文件,如下所示:http: //pastebin.com/JZyswTNX

I want to first import the file to my program, and then parse it into a 2-dimensional array of numbers rounded to 2 digits after the dot.我想首先将文件导入我的程序,然后将其解析为二维数字数组,在点后四舍五入为 2 位数字。

After that, i want to read only the last 20 lines of the txt file.之后,我只想读取 txt 文件的最后 20 行。 I prefer not to hard-code "go to line 480" because I'm not sure each file will have the same amount of lines.我不喜欢硬编码“转到第 480 行”,因为我不确定每个文件会有相同数量的行。

What would be the easiest way to do that?最简单的方法是什么? (some built ins maybe?) (可能是一些内置插件?)

You can use File.ReadAllLines which returns array of strings.您可以使用返回字符串数组的File.ReadAllLines Then you can split this array by comma然后你可以用逗号分割这个数组

var lines = File.ReadAllLines (path);
var result = lines.Select (x=>x.Split (','));

result is two dimensional array result是二维数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM