gpt4 book ai didi

C#逐行解析文本数据

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:30 27 4
gpt4 key购买 nike

我的项目有问题,让我在这里解释一下,我在这里需要一些专家指导,因为我是编程新手。我在记事本中有这样的数据:

10192 20351 30473 40499 50449 60234    
10192 20207 30206 40203 50205 60226
10192 20252 30312 40376 50334 60252

它是26行数据,但我只显示3行只是为了举例。这是一些按优先级排序的规则:

-我只想读取文本文件然后提取数字。例如:10192 20351 等等。

-我有 6 列的 ListView,我想在它的列中显示每一行数字

Column 1 | Column 2 | Column 3 |Column 4 | Column 5 | Column 6

10192   | 20351   | 30473  | 40499  |50449  | 60234

-当然,如果可能的话,每 5 位数字的前 2 位数字是唯一代码,我只需要最后 3 位数字。例如:192 351 473 499 234。因此每个数字都将以 10.000 为模数。

我想我让很多人感到困惑,抱歉,这是我当前的代码

私有(private)委托(delegate) void UpdateUiTextDelegate(String Text); private void serial_DataRecieved(对象发送者,System.IO.Ports.SerialDataReceivedEventArgs e) { //将接收到的字符收集到我们的“缓冲区”(字符串)中。 字符串接收到的数据; received_data = serial.ReadExisting(); Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(WriteData), received_data);

    private void WriteData(String Text)
{
if (bufferData != "" || Text[0] == '1')
bufferData += Text;
if (bufferData.Length >= 35)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Rads\Desktop\Training06.txt", true))
{
file.WriteLine(bufferData);
}
listBox1.Items.Add(bufferData);
bufferData = "";
}
}
#endregion

private void Window_Loaded(object sender, RoutedEventArgs e)
{

}

//Browse .txt file
private void Browse_btn_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

dlg.DefaultExt = ".txt";
dlg.Filter = "Text document (.txt)|*.txt";

Nullable<bool> result = dlg.ShowDialog();

if (result == true)
{
string filename = dlg.FileName;
textBox.Text = filename;
}
}

private void Parsing_String(string filename)
{
List<Row> list = new List<Row>();

foreach (String str in File.ReadLines(filename))
{
String[] strCols = str.Split(Convert.ToChar(" "));
list.Add(new Row()
{
Column1 = strCols[0].Substring(2),
Column2 = strCols[1].Substring(2),
Column3 = strCols[2].Substring(2),
Column4 = strCols[3].Substring(2),
Column5 = strCols[4].Substring(2),
Column6 = strCols[5].Substring(2),


});
}

dg.ItemsSource = list;
}

public class Row
{
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }

}

XAML 代码

<Window x:Class="SamplingData.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="368" Width="401" Loaded="Window_Loaded">

<TabControl Height="332" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="380">
<TabItem Header="Sampling" Name="Sampling">
<Grid>
<Label Content="DATA RECEIVED" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label1" VerticalAlignment="Top" />
<Button Content="Connect" Height="23" HorizontalAlignment="Left" Margin="264,6,0,0" Name="ConnectButton" VerticalAlignment="Top" Width="75" Click="Connect_Comms" />
<ListBox Height="222" HorizontalAlignment="Left" Margin="10,37,0,0" Name="listBox1" VerticalAlignment="Top" Width="329" />
</Grid>
</TabItem>
<TabItem Header="Training" Name="tabItem1">
<Grid>
<Button Content="Training" Height="23" HorizontalAlignment="Left" Margin="243,28,0,0" Name="Train_Btn" VerticalAlignment="Top" Width="75" />
<Button Content="Browse" Height="23" HorizontalAlignment="Left" Margin="243,6,0,0" Name="Browse_btn" VerticalAlignment="Top" Width="75" Click="Browse_btn_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="6,7,0,0" Name="textBox" VerticalAlignment="Top" Width="231" Background="{x:Null}"></TextBox>
<RadioButton Content="RadioButton" Height="16" HorizontalAlignment="Left" Margin="507,76,0,0" Name="radioButton1" VerticalAlignment="Top" />
<DataGrid x:Name="dg" AutoGenerateColumns="False" Margin="0,57,0,0" DataContext="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Column1}" Header="Column 1"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Column2}" Header="Column 2"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Column3}" Header="Column 3"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Column4}" Header="Column 4"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Column5}" Header="Column 5"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Column6}" Header="Column 6"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</TabItem>
</TabControl>

非常感谢,如果有人能帮助我..

最佳答案

我认为您根本不需要 Regex。你只需要 string.Split按行和按空格。

string[] lines = data.Split(Enviroment.NewLine);

对于每一行,您可以通过空格分割行来获取字段。

string[] fields = line.Split(' ');

关于C#逐行解析文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5660947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com