【问题标题】:Parse a string with key-value pairs where there is no char between key and value c#解析具有键值对的字符串,其中键和值之间没有字符 c#
【发布时间】:2018-03-27 20:57:21
【问题描述】:

我正在尝试逐行解析文件,每次将变量设置为等于行中的值。

示例行是:

G1 Z0.500 F7800.000
G1 X-0.336 Y13.564 F7800.000
G1 X3.205 Y13.493 E3.63071 F1800.000

我会有如下变量:

double x;
double y;
double z;

假设我解析第一行,我的输出应该是:

x = 100000;
y = 100000;
z = 0.5;

如果我解析第二行,我的输出应该是:

x = -0.336;
y = 13.564;
z = 100000;

第三行只是可能出现的另一行,但都非常相似。如果键未出现在字符串中,则值设置为 100,000。

我尝试使用这样的代码:

char[] delimiters = { 'X', 'Y', 'Z'};
string[] words = line.Split(delimiters);

但第一行返回:

z = 0.500 F7800.000

为这些键值对解析每一行的最佳方法是什么?

【问题讨论】:

  • 分割空格字符并寻找与您预期的 x,y,z 字符匹配的前导字符,然后将输入的其余部分转换为双精度
  • @Amy 是的,我尝试使用分隔符 X Y 和 Z 的字符数组,但是它当然会在分隔符之后返回整个字符串。
  • 请将您的尝试添加到问题中。
  • @RufusL 我并不一定意味着 null 。而是需要将其设置为我尚未决定的任意数字。
  • 我不会使用 x,y,z 作为分隔符。空格是分隔符。然后检查每个段开头的字符。

标签: c# string parsing split key-value


【解决方案1】:

这应该让你开始......

        using (StreamReader sr = new StreamReader("data.txt"))
        {
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                if (line != null)
                {
                    string[] items = line.Split(' ');
                    decimal? x, y, z = null;
                    for (int i = 1; i < items.Length; i++)
                    {
                        if (items[i].ToLower().StartsWith("x"))
                        {
                            x = decimal.Parse(items[i].Substring(1));
                            Console.WriteLine($"x = {x}");
                        }
                        else if (items[i].ToLower().StartsWith("y"))
                        {
                            y = decimal.Parse(items[i].Substring(1));
                            Console.WriteLine($"y = {y}");
                        }
                        else if (items[i].ToLower().StartsWith("z"))
                        {
                            z = decimal.Parse(str);
                            Console.WriteLine($"y = {z}");
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
        }

【讨论】:

  • 谢谢!这几乎是我前进的方向。
【解决方案2】:

您可以做的一件事是创建一个简单的类来表示您关心的数据,这些数据似乎是XYZ。然后您可以创建一个static 方法,该方法知道如何从字符串创建类的实例。

例如:

public class XYZData
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }

    public static XYZData Parse(string input)
    {
        var xyzData = new XYZData { X = 100000, Y = 100000, Z = 100000 };

        if (string.IsNullOrWhiteSpace(input)) return xyzData;

        var parts = input.Split();

        foreach (var part in parts)
        {
            double result;

            if (part.Length < 2 || 
                !double.TryParse(part.Substring(1), out result)) 
            {
                continue;
            }

            if (part.StartsWith("X", StringComparison.OrdinalIgnoreCase))
            {
                xyzData.X = result;
                continue;
            }
            if (part.StartsWith("Y", StringComparison.OrdinalIgnoreCase))
            {
                xyzData.Y = result;
                continue;
            }
            if (part.StartsWith("Z", StringComparison.OrdinalIgnoreCase))
            {
                xyzData.Z = result;
                continue;
            }
        }

        return xyzData;
    }
}

然后您可以在读取文件时填充这些内容的列表:

var filePath = @"f:\public\temp\temp.txt";

var data = new List<XYZData>();

foreach (var fileLine in File.ReadLines(filePath))
{
    data.Add(XYZData.Parse(fileLine));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2018-08-12
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多