首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重新排列txt文件中的数据

如何重新排列txt文件中的数据
EN

Stack Overflow用户
提问于 2019-03-30 20:40:23
回答 1查看 76关注 0票数 0

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

理想情况下,位置数据应该在一条线路上,电压数据在另一条线路上,但事实并非如此,有时数据之间也会有错误信息。

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

在手动排列txt文件中的数据时,首先对数据进行分离。

第一个数据是位置数据(例如,6130.0438232),它总是有12个字符(包括'.‘点),电压数据(看起来像0.0908446)有9个字符(包括’.‘)点)。请参阅附件中的图片以获取信息。

问题是我想手动做这件事,我想检查txt文件中的所有字符,并根据我的需要格式化它,如图所示。

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

Files are available in this link too

原始数据:

代码语言:javascript
复制
>
>
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
EN

回答 1

Stack Overflow用户

发布于 2019-04-01 09:40:01

为了好玩,我对你的问题做了第一次尝试。以下是基于提供的输入文件"RawData.txt"的输出

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

代码语言:javascript
复制
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();
    }
}

享受吧!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55431544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档