简体   繁体   English

如何将文本文件的内容转换为单个数组

[英]how to convert contents of a text file into individual array

this is my text file这是我的文本文件

Id:1
Source:86876786
Destination:878979723
Date:1/16/2021 3:31:22 PM
Status: Failed
Network :Jio

I have to store everything into individual array.I have to display like this id Source Destination Date Status Network我必须将所有内容存储到单独的数组中。我必须像这样显示 id Source Destination Date Status Network

If I was tasked with this I would perhaps have something to hold the data:如果我负责这个任务,我可能会有一些东西来保存数据:

public class X {
    public int Id { get; set; }
    public long Source { get; set; }
    public long Destination { get; set; }
    public DateTime TheDate { get; set; }
    public string Status { get; set; }
    public string Network { get; set; }
}

And then something to parse it:然后解析它:

var lines = File.ReadAllLines(path);

var exes = new List<X>();
var current = new X();

for(int i = 0; i < lines.Length; i++){
{
    var bits = lines[i].Split(":", 2);

    if (bits[0] == "Id") current.Id = int.Parse(bits[1]);
    else if (bits[0] == "Source") current.Source = long.Parse(bits[1]);
    else if (bits[0] == "Destination") current.Destination = long.Parse(bits[1]);
    else if (bits[0] == "Date") current.TheDate = DateTime.ParseExact(bits[1], "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
    else if (bits[0] == "Status") current.Status = bits[1].Trim();
    else if (bits[0] == "Network ") current.Network = bits[1];

    if(i % 6 == 5){
        exes.Add(current);
        current = new();
    }

}

It will tolerate lines in any order, but they have to come in groups of 6. If they will come in the same order, but might not be in groups of 6 (eg if the Status line is unknown then it's missing from the file) we can use a hash set to track if we've already read a value for the current item它将容忍任何顺序的行,但它们必须以 6 组为一组。如果它们以相同的顺序出现,但可能不是 6 组(例如,如果状态行未知,则文件中缺少它)我们可以使用 hash 设置来跟踪我们是否已经读取了当前项目的值

var exes = new List<X>();
var current = new X();
var hs = new HashSet<string>();

foreach (var line in lines)
{
    var bits = line.Split(":", 2);

    if (!hs.Add(bits[0]))
    {
        exes.Add(current);
        current = new();
        hs.Clear();
    }

    if (bits[0] == "Id") current.Id = int.Parse(bits[1]);
    else if (bits[0] == "Source") current.Source = long.Parse(bits[1]);
    else if (bits[0] == "Destination") current.Destination = long.Parse(bits[1]);
    else if (bits[0] == "Date") current.TheDate = DateTime.ParseExact(bits[1], "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
    else if (bits[0] == "Status") current.Status = bits[1];
    else if (bits[0] == "Network") current.Network = bits[1];

}

If they change order and don't come in groups of 6, you need something else to delimit groups.如果他们改变顺序并且不以 6 人一组的形式出现,则您需要其他东西来划分组。 Use JSON使用 JSON

-- --

Then just go through your List<X> and diplay them how you want.然后只需 go 通过您的List<X>并以您想要的方式显示它们。 Give X a better name too也给 X 起个好听的名字

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

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