如何重新整理txt文件中的数据

时间:2019-03-30 12:40:23

标签: c# parsing file-handling

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

假定位置数据最好在一行中,而电压数据最好在另一行中,但它们不是这样,有时数据之间也会出现错误消息。

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

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

第一个数据是位置数据(例如6130.0438232),该位置数据始终具有12个字符(包括“。”点),而电压数据(看起来像0.0908446)则总是9个字符(包括“。”点)。请参阅附件中的图片以获取信息。

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

我希望你们能对此提出建议。

enter image description here

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

1 个答案:

答案 0 :(得分:1)

为了娱乐,我尝试了一下您的问题。这是基于提供的输入文件"RawData.txt"

的输出

scr

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

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();
    }
}

享受!