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

C# 如何重新排列txt文件中的数据,c#,parsing,file-handling,C#,Parsing,File Handling,我正试图格式化一个原始数据反馈从我的伺服驱动器保存在一个txt文件。数据为每10ms伺服电机的位置和电压 理想情况下,位置数据在一条线路上,电压数据在另一条线路上,但它们不在同一条线路上,有时数据之间也会有错误消息 由于数据包含许多错误,有时两个数据被合并在一起,我在保存数据的txt文件中手动安排数据 在手动排列txt文件中的数据时,首先我分离数据 第一个数据是位置数据(例如6130.0438232),总是有12个字符(包括“.”点)和电压数据(看起来像0.0908446)9个字符(包括“.”点

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

理想情况下,位置数据在一条线路上,电压数据在另一条线路上,但它们不在同一条线路上,有时数据之间也会有错误消息

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

在手动排列txt文件中的数据时,首先我分离数据

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

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

我希望你们能给我一些建议

原始数据:

>
>
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

为了好玩,我第一次尝试解决你的问题。以下是基于提供的输入文件“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();
    }
}
公共结构数据点
{
//下面的字段默认为0,这些字段被解释为未被删除
//尚未设置。如果该值为负值,则表示错误,
//如果该值为正,则设置该值。
公众持股;
公共浮充电压;
公用常量字符串错误=“!FF10”;
//检查两个字段是否为非零(已设置)。
公共布尔IsOK=>位置!=0和电压!=0;
公共重写字符串ToString()
{
//将这两个字段转换为逗号分隔的字符串行
var pos_str=Position>0?Position.ToString():
位置==0?字符串。空:DataPoint.Error;
var vlt_str=电压>0?电压。ToString()
电压==0?字符串。空:数据点。错误;
返回$“{pos_str},{vlt_str}”;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
//处理数据并生成数据点数组
DataPoint[]输入=ProcessFile(File.OpenText(“RawData.txt”);
//在控制台中将数组中的项显示为表
int指数=0;
//宽度为5、12和9的三列
WriteLine($“{”Index“,-5}{”Pos“,-12}{”Volts“,-9}”);
foreach(输入中的数据点项)
{
索引++;
//每个数据点包含数据(浮点数),
//并可以使用转换为逗号分隔的字符串
//.ToString()方法。
var parts=item.ToString().Split(',');
WriteLine($“{index,-5}{parts[0],-12}{parts[1],-9}”);
}
}
静态数据点[]进程文件(StreamReader)
{
var list=新列表();
//由读卡器填写的当前数据
数据点数据=新数据点();
//跟踪下一个输入是否用于
//位置或电压。
bool expect\u position\u value=false;
而(!reader.EndOfStream)
{
var line=reader.ReadLine().Trim();
//每行为:
//*空白
//*位置数据,12个字符,数字
//*电压数据,9个字符,数字
//*错误代码“!FF10”
//但文件中存在随机换行。
if(string.IsNullOrEmpty(行))
{
//空行,什么也不做
继续;
}
if(第行开始时带(“>”)
{
//提示行,什么也不做
继续;
}
//在位置和电压之间翻转预期值
expect\u position\u value=!expect\u position\u value;
如果(!line.StartsWith(DataPoint.Error)
&&line.Length!=9&&line.Length!=12)
{
//数据被换行拆分。读取
//下一行并组合在一起。
var next=reader.ReadLine().Trim();
Debug.WriteLine(下一步);
行+=下一行;
}
if(line.StartsWith(DataPoint.Error))
{
//误差值
如果(预期位置值)
{
数据。位置=-1;
}
其他的
{
数据。电压=-1;
}
if(data.IsOK)
{
列表。添加(数据);
数据=新数据点();
expect\u position\u value=false;
}
继续;
}
如果(行长度==12)
{
//位置值
if(浮动三角板(线,外浮动位置))
{
数据。位置=位置;
expect\u position\u value=true;
}
其他的
{
//无法读取位置,现在怎么办?
}
}
如果(行长度==9)
{
//电压值
if(浮点数(线路、浮点数外电压))
{
数据。电压=电压;
expect\u position\u value=false;
}
其他的
{
//无法读取电压。现在怎么办?
}
}
if(data.IsOK)
{
//数据已填充。添加到列表并获取
//准备好下一个数据点。
列表。添加(数据);
数据=新数据点();
}
}
//导出数据数组。
return list.ToArray();
}
}
恩杰