1

我有一个包含以下数据的 txt 文件

(0010,0010) : Patient's Name                : LANE^LOIS^^^

(0010,0020) : Patient ID                    : AM-0053

(0010,0030) : Patient's Birth Date          : 4/15/1982

(0010,0040) : Patient's Sex                 : F

我必须逐行阅读内容并创建一个数据表,其中包含以下详细信息 Patient's Name,Patient ID,Patient's Birth Date,Patient's Sex。常量(例如(0010,0010))不会改变。它代表患者姓名。你能否给我这个任务背后的逻辑。我有这么多,

逐行阅读

获取前 11 个字符并检查是否为 (0010,0010)

转到行尾,或将行拆分:并获取数组的第二个元素。

我想得好吗?或者我怎样才能提高性能?

4

5 回答 5

2

你的方法听起来很明智。用“:”分割看起来是一个合理的想法。

这种字符串处理将非常快——而且比将结果数据记录写入磁盘或数据库要快得多,因此效率可能不应该成为问题。

于 2011-11-02T14:00:40.807 回答
2

在您知道存在问题之前不要担心性能,但一般来说,如果您可以避免过多的内存分配,这对您有利。因此,如果您只需要最后一部分,则可以StartsWith()在字符串上使用,这样您就不必创建稍后将被垃圾收集的子字符串,然后您可以使用它LastIndexOf()来查找最后一部分的开头并仅对剩余部分进行子串化。

while((line = Console.ReadLine()) != null)
{
    if (line.StartsWith("0010,0010"))
    {
        var pos = line.LastIndexOf(':');

        if (pos != -1)
        {
            // do whatever with part
            var part = line.SubString(pos+1).Trim();
        }    
    }
}
于 2011-11-02T14:02:59.520 回答
1

如果它包含 (0010,0010) 或(4 位后跟 ,以及另外 4 位),您甚至可以在拆分之前检查您的线路。如果检测到,您可以拆分为字符串数组、修剪空格并填充表格行。您可以使用以下表达式查找 (0010,0010)

Regex.IsMatch("line string here...", "[(]{1}[0-9]{4},{1}[0-9]{4}[)]{1}") // should be true if found
于 2011-11-02T14:02:42.817 回答
1

这似乎是一个合理的方法。在它成为问题之前,我不会担心性能。

为了论证的缘故,我们假设您有 100,000 个。先写一些工作代码,然后用 aSystem.Diagnostics.Stopwatch来计算 100 需要多长时间。找到流程中运行时间最长的部分,并尝试缩短它。可能是(我还没有尝试过)逐行读取文件。您可以尝试一口气读取文件,然后将其拆分为换行符。使用处理器的所有内核并行运行它们可能会更好。

于 2011-11-02T14:02:59.043 回答
1

这个小方法应该可以解决大多数问题。:) 它循环遍历行(您必须调整循环并将其替换为文本阅读器)

将所有内容放入患者列表中。

void Main()
{
    var input = @"(0010,0010) : Patient's Name                : LANE^LOIS^^^
    (0010,0020) : Patient ID                    : AM-0053
    (0010,0030) : Patient's Birth Date          : 4/15/1982
    (0010,0040) : Patient's Sex                 : F
    (0010,0010) : Patient's Name                : LANE^LOIS^^^
    (0010,0020) : Patient ID                    : AM-0053
    (0010,0030) : Patient's Birth Date          : 4/15/1982
    (0010,0040) : Patient's Sex                 : F
    (0010,0010) : Patient's Name                : LANE^LOIS^^^
    (0010,0020) : Patient ID                    : AM-0053
    (0010,0030) : Patient's Birth Date          : 4/15/1982
    (0010,0040) : Patient's Sex                 : F";
    List<Patient> patients = new List<Patient>();

    Patient p = null;
    foreach(var line in input.Split(new[] {'\n'}))
    {
        var value = line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Last().Trim();
        if(line.Trim().StartsWith("(0010,0010)"))
        {
            if(p != null)
                patients.Add(p);
            p = new Patient();
            p.Name = value;
        }
        else if(line.Trim().StartsWith("(0010,0020)"))
        {
            p.ID = value;
        }
        else if(line.Trim().StartsWith("(0010,0030)"))
        {
            DateTime birthDate;
            if(DateTime.TryParse(value, out birthDate))
                p.BirthDate = birthDate;
        }
        else if(line.Trim().StartsWith("(0010,0040)"))
        {
            p.Sex = value.ToCharArray()[0]; 
        }
    }
    if(p != null)
        patients.Add(p);
}

public class Patient
{
    public string Name { get; set; }
    public string ID { get; set; }
    public DateTime? BirthDate { get; set; }
    public char Sex { get; set; }
}
于 2011-11-02T14:23:53.213 回答