0

2 つのテキスト ファイルがあります。各テキスト ファイルのフィールドは、スペース (" ") で区切られています。ファイル 1 の列 1 のフィールドの一部は、ファイル 2 の列 1 のフィールドと一致します。ただし、ファイル 2 の 3 番目の列は数値フィールドです。私がやりたいことは、ファイル 1 のすべてのフィールドをファイル 2 のフィールドに対してチェックし、数値が 1 の場合はファイル 2 から行を削除し、数値が > 1 の場合はそこから 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 に答える