【问题标题】:How to rearrage the data which is in a txt file如何重新排列文本文件中的数据
【发布时间】:2019-03-30 12:40:22
【问题描述】:

我正在尝试格式化保存在 txt 文件中的伺服驱动器的原始数据反馈。数据是每 10 ms 伺服电机的位置和电压。

理想情况下,位置数据应位于一行中,而电压数据应位于另一行中,但事实并非如此,有时数据之间也会出现错误消息。

由于数据包含许多错误,有时两个数据会合并在一起,因此我将数据手动排列在保存数据的 txt 文件中。

在手动排列txt文件中的数据时,我先将数据分开。

第一个数据是位置数据(例如6130.0438232),它总是有12个字符(包括'.'点)和电压数据(看起来像0.0908446)9个字符(包括'.'点)。有关信息,请参阅所附图片。

问题是我想手动执行此操作,并想检查txt文件中的所有字符并根据我的需要进行格式化,如图所示。

希望大家给点建议

Files are available in this link too

原始数据:

>
>
6130.0
438232
0.0910353
!FF10
0.0910317

!FF10

!FF10

!FF10

!FF10

6130.0438232
!
FF10
6130.0438232
0.0908446

6130.0438232
0.1517510
6130.0438232
0.
1518797

613
0.0438232
0.1136887
6130.0438232
0.1133942

6130.0438232
0.0917661
6130.0438232
!FF10

32
5.7181644
!FF02
0.0912833

6130.0438232
!FF10
!FF10
0.0910270

6130.
0438232
0.0907409
6130.0438232
0.0907421

6130.043823
2
0.0906980
6130.0438232
0.0906491

6130.0438232
0.
1557195
6130.0438232
!FF10

6130.0438232
0.09
08780
!FF10
0.0908589

6130.0438232
0.0905549
6130.0438232
0.
0905442

【问题讨论】:

  • 将原始数据发布为代码 sn-p。
  • 你说:“第一个数据是位置数据(例如6130.0438232),它总是有12个字符(包括'.'点)”但它总是在点之前有四位数字吗?或者它总是十二个字符,在这十二个字符中anywhere
  • 位置数据为6130.04388232 mm(数值也包含十进制数字)实际上上面的数据中有12个字符代表舵机的位置(11个数字和一个点)。
  • 没关系。 325 行只有 11 个字符,而不是 12 个字符。另外,还有哪些错误信息可能?我看到!FF10!FF02总是 !F 后跟两位数?你必须非常具体。
  • 是的,错误总是跟在 !F 之后,并且 325 行(我找不到确切的行)可能缺少一些数据,这就是它的 11 个字符的原因。实际上,我必须删除这些错误,并且必须将插值数据放在无效值中。

标签: c# parsing file-handling


【解决方案1】:

为了好玩,我第一次尝试解决您的问题。这是基于提供的输入文件"RawData.txt"的输出@

以下是用于生成此输出的代码:

public struct DataPoint
{
    // The fields below default to 0 which are interpreted as hadn't been
    // set yet. If the value is negative then it represents an error,
    // and if the value is positive it is set.

    public float Position;
    public float Voltage;
    public const string Error = "!FF10";

    // Check the both fields are non-zero (have been set).
    public bool IsOK => Position!=0 && Voltage!=0;

    public override string ToString()
    {
        // Convert the two fields into a comma separated string line

        var pos_str = Position>0 ? Position.ToString() :
            Position==0 ? string.Empty : DataPoint.Error;
        var vlt_str = Voltage>0 ? Voltage.ToString() :
            Voltage==0 ? string.Empty : DataPoint.Error;

        return $"{pos_str},{vlt_str}";
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Process in the data and generate an array of DataPoint
        DataPoint[] input = ProcessFile(File.OpenText("RawData.txt"));

        // Display the items in the array as a table in the console
        int index = 0;
        // Three columns with widths, 5, 12 and 9
        Console.WriteLine($"{"Index",-5} {"Pos",-12} {"Volts",-9}");
        foreach (DataPoint item in input)
        {
            index++;
            // Each DataPoint contains data (floating point numbers),
            // and can be converted into a comma separated string using
            // the .ToString() method.
            var parts = item.ToString().Split(',');
            Console.WriteLine($"{index,-5} {parts[0],-12} {parts[1],-9}");
        }

    }

    static DataPoint[] ProcessFile(StreamReader reader)
    {
        var list = new List<DataPoint>();
        // current data to be filled by reader
        DataPoint data = new DataPoint();
        // keep track if the next input is for
        // position or voltage.
        bool expect_position_value = false;
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine().Trim();
            // each line is either:
            // * blank
            // * position data, 12 char, numeric
            // * voltage data, 9 char, numeric
            // * error code "!FF10"

            // but random line feeds exist in the file.
            if (string.IsNullOrEmpty(line))
            {
                // empty line, do nothing
                continue;
            }
            if (line.StartsWith(">"))
            {
                // prompt line, do nothing
                continue;
            }
            // flip the expected value between position & voltage
            expect_position_value=!expect_position_value;
            if (!line.StartsWith(DataPoint.Error) 
                    && line.Length!=9 && line.Length!=12)
            {
                // Data was split by a line feed. Read
                // next line and combine together.
                var next = reader.ReadLine().Trim();
                Debug.WriteLine(next);
                line+=next;
            }
            if (line.StartsWith(DataPoint.Error))
            {
                // Error value
                if (expect_position_value)
                {
                    data.Position=-1;
                }
                else
                {
                    data.Voltage=-1;
                }
                if (data.IsOK)
                {
                    list.Add(data);
                    data=new DataPoint();
                    expect_position_value=false;
                }
                continue;
            }
            if (line.Length==12)
            {
                // position value
                if (float.TryParse(line, out float position))
                {
                    data.Position=position;
                    expect_position_value=true;
                }
                else
                {
                    // cannot read position, what now?
                }
            }
            if (line.Length==9)
            {
                // voltage value
                if (float.TryParse(line, out float voltage))
                {
                    data.Voltage=voltage;
                    expect_position_value=false;
                }
                else
                {
                    // cannot read voltage. what now?
                }
            }
            if (data.IsOK)
            {
                // data has been filled. Add to list and get
                // ready for next data point.
                list.Add(data);
                data=new DataPoint();
            }
        }
        // Export array of data.
        return list.ToArray();
    }
}

享受吧!

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2016-08-02
    • 1970-01-01
    相关资源
    最近更新 更多