从文本文件转换成并行阵列分割数据(Splitting data from a text file i

2019-10-29 04:00发布

我的教授给类的C#,可用于将数据从一个文本文件分割的例子。 我试图将它用于涉及分裂一个txt的内容的项目。 文件到4个阵列或字段。 下面是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{
    static void Main()
    {
        int i = 0;
        foreach (string line in File.ReadAllLines("census.txt"))
        {
            string[] parts = line.Split(',');
            foreach (string part in parts)
            {
                Console.WriteLine("{0}",

                    part);
            }
            i++; 
        }
    }
}

这里是census.txt:

21,f, s, 14

41,f, m, 22

12, m, s, 12

11, f, s, 8

29, m, m, 4

6, m, s, 12

9, f, s, 2

30, f, s, 1

它应该是假设的人口普查数据按年龄,性别,婚姻状况,和地区去。 我不断收到输出是每个这些数字或字符在像这样一行:

21

f

s

14

41

f

m

22

等等。

我认为,这意味着它的工作,但我想知道如何用这个进入4个并行阵列。 我也想知道更多关于拆分为4个区域,结构,或类。 该项目的下一部分涉及到每一个一定年龄号码或区号码出现,这将涉及大量阵列计时。

Answer 1:

泛型列表(如这里的其他2个电流答案使用)是最好的一段路要走。 但是,如果您需要在阵列中的数据(如以前的问题似乎表明),那么你可以修改你这样的教授的代码:

C#

int[] districtDataD = new int[900];
string[] districtDataG = new string[900];
string[] districtDataM = new string[900];
int[] districtDataA = new int[900];

int i = 0;
foreach (string line in File.ReadAllLines("census.txt"))
{
    string[] parts = line.Split(',');

    districtDataD[i] = int.Parse(parts[0]);
    districtDataS[i] = parts[1];
    districtDataM[i] = parts[2];
    districtDataA[i] = int.Parse(parts[3]);
    i++;
}

VB.NET(因为你原来的问题被标记用VB.NET):

Dim districtDataD() As New Integer(900)
Dim districtDataS() As New String(900)
Dim distrcitDataM() As New String(900)
Dim districtDataA() As New Integer(900)

Dim i As Integer = 0

For Each Dim line As String In File.ReadAllLines("census.txt")
    Dim string() As parts = line.Split(',')

    districtDataD(i) = Integer.Parse(parts(0))
    districtDataS(i) = parts(1)
    districtDataM(i) = parts(2)
    districtDataA(i) = Integer.Parse(parts(3))

    i++
Next

你也可以使用一个structclass ,并有一个阵列,其保存的对象,但它看起来像你的教授要你使用4个独立的阵列。 如果你可以使用一个,你可以简单地声明数组这样的,例如:

C#

Person[] districtData = new Person[900];

VB.NET

Dim districtData() As New Person(900)

然后,你可以做到这一点,分裂逻辑内(注意,如果说Distric和年龄都在你的对象整数你必须投或解析他们为我展示如下):

C#

districtData[i] = new Person() { District = int.Parse(parts[0]), Gender = parts[1], MaritalStatus = parts[2], Age = int.Parse(parts[3]) };

VB.NET

districtData[i] = new Person() With { .District = Integer.Parse(parts[0]), .Gender = parts[1], .MaritalStatus = parts[2], .Age = Integer.Parse(parts[3]) }

有与此代码,如果你有900多条数据线,你会得到一个索引超出范围异常的风险。 避免这种情况的一种方法是修改我用while循环,检查目标阵列(一个或多个)的范围把上面的代码或线没有被超过,这样的数:

C#

string[] lines = File.ReadAllLines("census.txt");
int i = 0;

while (i < 900 && i < parts.Length)
{

    // split logic goes here
}

VB.NET

Dim lines As String() = File.ReadAllLines("census.txt")
Dim i As Integer = 0

While (i < 900 AndAlso i < lines.Length)

    ' split logic goes here
End While

我没有测试的代码,但这个希望能帮助你,如果你必须使用数组。



Answer 2:

我将延长irsog的回答一下:

  • 使用一个类,而不是一个结构
  • 使用特性,而不是场
  • 使用GenderMaritalStatus枚举普通字符串代替

码:

public class Person
{
    public int Age { get; set; }
    public MaritalStatus MaritalStatus { get; set; }
    public Gender Gender { get; set; }
    public int District { get; set; }
}

public enum MaritalStatus
{
    Single, Married
}

public enum Gender
{
    Male, Female
}

与用法:

var people = new List<Person>();

foreach (string line in File.ReadAllLines("Input.txt"))
{
    string[] parts = line.Split(',');

    people.Add(new Person()  {
        Age = int.Parse(parts[0]),
        MaritalStatus = parts[1] == "s" ? MaritalStatus.Single : MaritalStatus.Married,
        Gender = parts[2] == "m" ? Gender.Male : Gender.Female,
        District = int.Parse(parts[3])
    });
}


Answer 3:

这是古老的线程,但谷歌表示它前几页中我决定把我的意见。 我强烈反对给予TXT格式文件,因为它没有防止出错。 如果census.txt不能保证是理想的,尤其是如果它应该通过一些第三方(用户,管理员,无论是谁)来创建那么我强烈建议记录一些符号来结束,这样的:21,F,S ,14;

41,F,M,22; 那么第一件事情我们做什么 - 我们得到的记录数组,像这样:

串[]行= text.split( ';');

然后只需再次分裂 - 这个时间去记录元素。

的foreach(在串行记录)

{

串[]字段= record.split( '');

}

通过这种方式,不仅更容易阅读记录/字段,但你也可以很容易地检查文件的一致性,忽略错误(空记录),检查每个记录等领域的数



Answer 4:

你可以为需要的信息的结构:

public struct Info
{
    public int Age;
    public string gender;
    public string status;
    public int district;
}

和插入数据到您的结构清单:

  List<Info> info = new List<Info>();
    foreach (string line in File.ReadAllLines("census.txt"))
    {
        string[] parts = line.Split(',');

            info.Add(new Info() {Age=int.Parse(parts[0]), gender=parts[1], status=parts[2], district=int.Parse(parts[3]) });
    }

现在哟拥有人信息的列表。



文章来源: Splitting data from a text file into parallel arrays