C# - 解析/格式化 .txt 文件
所以我有一些 .txt 文件的格式是我不喜欢的。我想读取该文件并通过单击 GUI 中的一个按钮(或 2 个)来重新格式化它。另外,我希望能够通过单击另一个按钮来重新保存包含许多选项的文件。另外,如果可能的话,我希望原始文件显示在 GUI 左侧的富文本框中,一旦单击格式按钮,它将在 GUI 的右侧显示新文本一个单独的富文本框。
所以我目前有一个功能正常的“打开文件”按钮、“保存文件”按钮和“清除文本”按钮。但是,我需要一个“设置文本格式”按钮(除非我们可以将“打开文件”按钮和“设置文本格式”按钮合并为一个按钮!)...
这是文件传入时的样子。 https://i.sstatic.net/mlSMm.png
这就是我想要的看起来就像我单击格式时一样。 https://i.sstatic.net/1IzKF.png
我还有一个 GUI制作并打开并保存文件,我有以下代码:
private void openFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.DefaultExt = "*.txt";
openFile.Filter = ".txt Files|*.txt";
openFile.InitialDirectory = "C:\\";
openFile.RestoreDirectory = true;
try
{
if(openFile.ShowDialog() == DialogResult.OK && openFile.FileName.Length > 0)
{
openedTextRichTextBox.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);
}
else
throw new FileNotFoundException();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void saveFileButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.DefaultExt = "*.txt";
saveFile.Filter = ".txt Files|*.txt";
saveFile.InitialDirectory = "C:\\";
saveFile.RestoreDirectory = true;
try
{
if(saveFile.ShowDialog() == DialogResult.OK && saveFile.FileName.Length > 0)
{
formattedTextRichTextBox.LoadFile(saveFile.FileName, RichTextBoxStreamType.PlainText);
}
else
throw new FileNotFoundException();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
好的,实际问题是:
如何格式化传入的txt文件以删除除(不包括)标记为“level”、“number ref”的列之外的所有内容, “组成项目”, “描述”。这意味着,“---”下的所有内容,直到我打另一个“---”。在我点击另一个“---”后,我需要抓取与上面相同的列。这更有意义吗?我希望它看起来如何的示例位于第二个链接中。
So I have some .txt files that have been formatted in a way that I do not like. I want to read the file and reformat it by a click of a button (or 2) in my GUI. Also, I would like to be able resave the file with many options with a click of another button. Also, if it is possible I would like to have the original file be displayed in a rich text box on the left side of my GUI, and once the format button is clicked it will display the new text on the right side of my GUI in a seperate rich text box.
So I currently have a functioning "Open File" button, "Save File" button, and "Clear Text" button. However, I need a "Format Text" button (unless we can combine the open file button and the format text button into just one button!)...
Here is what the file will look like when it comes in.
https://i.sstatic.net/mlSMm.png
And this is what I want it to look like when I click format.
https://i.sstatic.net/1IzKF.png
I also have a GUI that I have made and to open and save the file I have the following code:
private void openFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.DefaultExt = "*.txt";
openFile.Filter = ".txt Files|*.txt";
openFile.InitialDirectory = "C:\\";
openFile.RestoreDirectory = true;
try
{
if(openFile.ShowDialog() == DialogResult.OK && openFile.FileName.Length > 0)
{
openedTextRichTextBox.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);
}
else
throw new FileNotFoundException();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void saveFileButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.DefaultExt = "*.txt";
saveFile.Filter = ".txt Files|*.txt";
saveFile.InitialDirectory = "C:\\";
saveFile.RestoreDirectory = true;
try
{
if(saveFile.ShowDialog() == DialogResult.OK && saveFile.FileName.Length > 0)
{
formattedTextRichTextBox.LoadFile(saveFile.FileName, RichTextBoxStreamType.PlainText);
}
else
throw new FileNotFoundException();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Okay so the actual question is:
How do I format the incoming txt file to delete everything except for (not including) the columns labeled "level", "number ref", "component item", "description". This meaning, everything under the "---" until I hit another "---". After I hit another "---" I need to grab the same columns as above. Does this make more sense? The example of how I want it to look is in the second link.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过正则表达式运行文本,选出感兴趣的行:
如果您不熟悉正则表达式,则应该研究它们,例如这里: http://www.regular-expressions.info/
Run the text through a regular expression that picks out the lines of interest Something along these lines:
If you are not familiar with regular expressions, you should look into them, for example here: http://www.regular-expressions.info/