0

我有两个文本文件。每个文本文件中的字段由空格 (" ") 分隔。文件 1 的第 1 列中的某些字段与文件 2 的第 1 列中的字段匹配。但是,文件 2 中的第三列是数字字段。我想要做的是检查 file1 中的每个字段与文件 2 中的字段,如果数字为 1,则从文件 2 中删除该行,如果数字大于 1,则从中减去一个。

到目前为止,我有以下编码。

Dim lines1 As New List(Of String)(IO.File.ReadAllLines("File1"))
Dim lines2 As New List(Of String)(IO.File.ReadAllLines("File2"))

Dim values As New Dictionary(Of String, Integer)()
For Each line As String In lines1
    Dim fields() As String = line.Split(ControlChars.Tab)
    If fields.Length > 1 Then
        values(fields(0)) = Integer.Parse(fields(1))
    End If
Next

For Each line As String In lines2
    Dim fields() As String = line.Split(ControlChars.Tab)
    If fields.Length > 0 Then
        If values.ContainsKey(fields(0)) > 1 Then
            values(fields(0)) = values(fields(0)) - 1
        Else
            values.remove(fields(0))
        End If
    End If
Next

lines1.Clear()
For Each pair As KeyValuePair(Of String, Integer) In values
    lines1.Add(pair.Key + ControlChars.Tab + pair.Value.ToString())
Next

IO.File.WriteAllLines("File2", lines1.ToArray)

例如

File1

String1 String 
String2 String
String5 String
String8 String

File2

String1 String 6
String2 String 8
String3 String 2
String4 String 2
String5 String 1
String6 String 4
String7 String 8
String8 String 1

在我的代码运行之后

File1

String1 String 
String2 String
String5 String
String8 String

File2

String1 String 5
String2 String 7
String3 String 2
String4 String 2
String6 String 4
String7 String 8
4

1 回答 1

1

这里有一些解析的乐趣......只需使用它们各自的文件路径调暗 strFile1Path 和 strFile2Path 并让此代码处理其余部分。希望代码和注释能教给你一些技巧。

    Dim lstFile1Contents As New List(Of String)(IO.File.ReadAllLines(strFile1Path))
    Dim lstFile2Contents As New List(Of String)(IO.File.ReadAllLines(strFile2Path))

    Dim sbNewFile2Contents As New System.Text.StringBuilder

    For Each strLineToProcess As String In lstFile2Contents

        'Trim off trailing spaces for processing.
        strLineToProcess = Trim(strLineToProcess)

        Dim strCheckForMatch As String = strLineToProcess.Substring(0, (InStr(strLineToProcess, " ") - 1))

        Dim bolFoundMatch As Boolean = False
        Dim intCursor As Integer = 0
        Do Until intCursor = lstFile1Contents.Count OrElse bolFoundMatch

            If lstFile1Contents(intCursor).Substring(0, (InStr(lstFile1Contents(intCursor), " ") - 1)) = strCheckForMatch Then

                bolFoundMatch = True

                'We found a match, so let's check the third field.
                Dim intNumber As Integer = CInt(strLineToProcess.Substring((strLineToProcess.LastIndexOf(" ") + 1), (Len(strLineToProcess) - (strLineToProcess.LastIndexOf(" ") + 1))))

                If intNumber > 1 Then

                    'Subtract one from the third field.
                    sbNewFile2Contents.AppendLine(strLineToProcess.Substring(0, (strLineToProcess.LastIndexOf(" ") + 1)) & (intNumber - 1).ToString())

                End If

            End If

            intCursor += 1

        Loop

        If Not bolFoundMatch Then

            'No match was found, so make sure the line remains unedited.
            sbNewFile2Contents.AppendLine(strLineToProcess)

        End If

    Next

    'Finally write the file contents.
    IO.File.WriteAllText(strFile2Path, sbNewFile2Contents.ToString())
于 2012-06-21T16:57:35.483 回答