将分隔的文本文件读取到数组 [英] Reading delimited text file to an array

查看:60
本文介绍了将分隔的文本文件读取到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我使用带有定界符"(空的空格)将文本文件读取到数组的语法吗.

Can anyone help me out with the syntax of reading a text file to an array with a delimiter " "(Empty space).

推荐答案

复杂也是一种简单的解决方案.

如果文件很小,那么解决方案可能是非常基本的

This can be a complex as also an easy solution.

If you file is a small one, then a solution can be very basic

string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
string[] splittedText = text.Split('' '');



甚至是一行



or even in one line

string[] splittedText = File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt").Split('' '');



否则,请更准确地处理请求,我会为您指明正确的方向.

干杯



根据评论,这里有两个快速解决方案,

第一个,如果您没有管理可能的转换错误



Otherwise, be more precise on the request and I will point you to the proper direction.

Cheers



Based on the comments here are two quick solutions,

first one, if you are not managing the possible conversion errors

string[] splittedText = File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt").Split(' ');
int[] numbersArray = Array.ConvertAll<string, int>(splittedText, delegate(string str) { return int.Parse(str); });



但是正如已经提到的,如果存在无法解析的字符串,这会产生异常.

或更正统的方式:



But as already mentioned, this wel generate an exception if there is a string that can''t be parsed...

Or a more orthodox way:

string[] splittedText = File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt").Split(' ');

List<int> numbers = new List<int>();
int b;

foreach (string digit in splittedText)
{
    if (int.TryParse(digit, out b))
        numbers.Add(b);
}

int[] numbersArray = numbers.ToArray();



希望对您有所帮助.



Hope this helps.


这取决于文件的大小以及要付出的努力.如果文件不大,则可以使用流阅读器的ReadToEnd [ ^ ]到将整个内容读取为字符串,然后使用 String.Split [ ^ ]来完成您的任务.

如果文件很大,则需要缓冲从StreamReader获取数据以节省内存:
It depends on the size of your file and how much effort you want to put. If the files aren''t large you can use Stream Reader''s ReadToEnd[^] to read the entire contents to a string, then use String.Split[^] to fulfill your task.

If the files are large you need to buffer getting the data from the StreamReader, to conserve memory:

  1. 如果您实际上是逐行读取数据(例如,而不是解析文本),则可以使用StreamReader.ReadLine分别处理每条记录,这样可以减少内存占用.
  2. 读取一组数据一次将字节数(例如,使用ReadBlock方法)存储到字符串中并将其拆分.此方法的一个缺点是,最后一项可能会在两次缓冲区读取中分开,因此您需要保留块 n 的最后一项,最后一个字符不是空格,并且如果第一个字符不是
  3. ,则追加块 n + 1 的第一项.您一次只能读取一个字节/字符,但这比选项2IIRC慢得多.如果使用此方法,则只需为当前单词构建一个字符串,然后在下一个空格处将其添加到数组中即可.

  1. If you are actually reading data line-by-line (rather than parsing text for example) you can use StreamReader.ReadLine to process each record individually, this reduces the memory hit.
  2. Read a set of bytes at a time (using the ReadBlock method for example) getting the results into a string and splitting them. One drawback of this method is that it is likely that the last item item will be split across two buffer reads, so you will need to keep the last item of block n the last character is not a space and append the first item of block n+1 if its first character is not
  3. You can just read a byte/char at a time, but this is much slower than option 2IIRC. If you use this method you can just build a string for the current word and add it to the array once you hit the next space.


希望对您有帮助


Hope this helps


尝试
char[] sep = new char[] { ' ' };
myArray = myText.Split(sep);


这篇关于将分隔的文本文件读取到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆