重写从txt文件收集的数据?

发布于 2024-10-10 16:07:59 字数 450 浏览 3 评论 0原文

我有一个由垂直列表组成的 txt 文件。该列表包含 x 个数量的组,但标准是它们始终由三个实体组成。

示例如下:

PANNEL A
38
2440

解释为:

NAME
WIDTH
LENGTH

三个项目的列表(尽管列表可以包含 x 数量)等同于:

NEW BEAM
38
2440
WOOD
22
610
ITEM A
50
1220

我现在需要创建一个新的 txt 文件,其写法如下:

(“NEW BEAM”  is “38” x “2440”)
(“WOOD”  is “22” x “610”)
(“ITEM A”  is “50” x “1220”)

我如何使用 VB.NET 执行此操作?

I have a txt file comprised of a vertical list. The list contains x-amount of groups but what is standard is that they are always made up three entities.

Example as follows:

PANNEL A
38
2440

Explained as:

NAME
WIDTH
LENGTH

A list of three items (though list can contain x-amounts) is identical to:

NEW BEAM
38
2440
WOOD
22
610
ITEM A
50
1220

I now need to create a new txt file, which is written as follows:

(“NEW BEAM”  is “38” x “2440”)
(“WOOD”  is “22” x “610”)
(“ITEM A”  is “50” x “1220”)

How van I do this using VB.NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-10-17 16:07:59

我的做法如下:

Private Sub DoWork(ByVal inputFile As String, ByVal outputFile As String)
    '//The VB IDE confuses smart quotes with regular quotes so it gets hard to embed them in a string.
    '//Instead we'll embed them as raw characters
    Static LQ As Char = Chr(147)
    Static RQ As Char = Chr(148)

    '//Load our input file specifying that we want to read it and that we're willing to share it with other readers
    Using FSInput As New System.IO.FileStream(inputFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
        '//Bind a string-based reader to the stream. If you know the character encoding of the file you might want to specify that as the second paramter
        Using SR As New System.IO.StreamReader(FSInput, True)
            '//Create a stream to the output file specifying that we're going to write to it but other people can still read from it
            Using FSOutput As New System.IO.FileStream(outputFile, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
                '//Create our output file using the UT8 encoding, this can be changed to something else if needed
                Using SW As New System.IO.StreamWriter(FSOutput, System.Text.Encoding.UTF8)
                    '//Read the first line
                    Dim curLine = SR.ReadLine()
                    '//Create a loop that uses the curLine variable as well as the next two lines.
                    '//This method won't fail if there's bad data but might produce erroneous output
                    Do While Not String.IsNullOrEmpty(curLine) '//Use IsNullOrEmpty because the last line might be a blank line
                        '//Write our line of text
                        SW.WriteLine("({0}{2}{1}  is {0}{3}{1} x {0}{4}{1})", LQ, RQ, curLine, SR.ReadLine(), SR.ReadLine())
                        '//Read the next line and send back to the top of the loop
                        curLine = SR.ReadLine()
                    Loop
                End Using
            End Using
        End Using
    End Using
End Sub

您可以通过以下方式调用此方法:

DoWork("C:\Input.txt", "C:\Output.txt")

Here's how I'd go about it:

Private Sub DoWork(ByVal inputFile As String, ByVal outputFile As String)
    '//The VB IDE confuses smart quotes with regular quotes so it gets hard to embed them in a string.
    '//Instead we'll embed them as raw characters
    Static LQ As Char = Chr(147)
    Static RQ As Char = Chr(148)

    '//Load our input file specifying that we want to read it and that we're willing to share it with other readers
    Using FSInput As New System.IO.FileStream(inputFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
        '//Bind a string-based reader to the stream. If you know the character encoding of the file you might want to specify that as the second paramter
        Using SR As New System.IO.StreamReader(FSInput, True)
            '//Create a stream to the output file specifying that we're going to write to it but other people can still read from it
            Using FSOutput As New System.IO.FileStream(outputFile, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
                '//Create our output file using the UT8 encoding, this can be changed to something else if needed
                Using SW As New System.IO.StreamWriter(FSOutput, System.Text.Encoding.UTF8)
                    '//Read the first line
                    Dim curLine = SR.ReadLine()
                    '//Create a loop that uses the curLine variable as well as the next two lines.
                    '//This method won't fail if there's bad data but might produce erroneous output
                    Do While Not String.IsNullOrEmpty(curLine) '//Use IsNullOrEmpty because the last line might be a blank line
                        '//Write our line of text
                        SW.WriteLine("({0}{2}{1}  is {0}{3}{1} x {0}{4}{1})", LQ, RQ, curLine, SR.ReadLine(), SR.ReadLine())
                        '//Read the next line and send back to the top of the loop
                        curLine = SR.ReadLine()
                    Loop
                End Using
            End Using
        End Using
    End Using
End Sub

And you'd call this method by:

DoWork("C:\Input.txt", "C:\Output.txt")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文