在文本文件中搜索特定值 [英] search for a specific value in text files

查看:28
本文介绍了在文本文件中搜索特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须列出许多变体.第一个列表样本

列表 1)

hkdhksa
OP-ID:111112
jklfjdlkfsd
hfldhfjksdf
OP-ID:111113
ghjg
OP-ID:111114
OP-ID:111115
gjgjhghgjhg
OP-ID:111116
OP-ID:111117
OP-ID:111118

列表 2)

OP-ID:111112
OP-ID:11113
OP-ID:111114
OP-ID:111115
OP-ID:111117

结果将是:OP-ID:11118 不在列表 2 中

Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) 处理 Button1.Click'声明两个字典.每个人的关键是' 从输入行到的文本,'但不包括第一个,".' 每个的值将是整个输入行.'Dim file1 As New HashSet(Of String) '!Dim file1 As New Dictionary(Of String, String)Dim file2 作为新字典(字符串,字符串)对于 System.IO.File.ReadAllLines(TEST1) 中的每一行作为字符串Dim part() As String = line.Split(",")If line = ("OP-ID:") Like "OP-ID:*" Then如果不是 file1.ContainsKey(part(0)) 然后 file1.Add(part(0), line)万一下一个对于 System.IO.File.ReadAllLines(TEST2) 中的每一行作为字符串Dim part() As String = line.Split(",")如果不是 file2.ContainsKey(part(0)) 然后 file2.Add(part(0), line) '!下一个Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys)Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)Dim str = String.Join(vbCrLf, values)txtResults.Text = ("ID 不应在列表中:" & str)结束子

解决方案

您的示例列表没有任何逗号,从问题中不清楚它们是否存在.

如果没有逗号:

如果 TEST1 中没有重复项(或者您不关心它们):

Dim hs As New Hashset(Of String)(File.ReadLines(TEST1))hs.ExceptWith(File.ReadLines(TEST2))txtResults.Text = $"ID 不在列表中:{vbCrLf}{String.Join(vbCrLf, hs)}"

如果重复很重要:

Dim hs As New Hashset(Of String)(File.ReadLines(TEST2))Dim notFound = File.ReadLines(TEST1).Where(Function(x) Not hs.Contains(x)).ToListtxtResults.Text = $"ID 不在列表中:{vbCrLf}{String.Join(vbCrLf, notFound)}"

<小时>

如果有逗号

(共享代码)

Dim keyParser = Function(x As String)Dim indexOf = s.IndexOf(如果 indexOf = -1 然后返回 x返回左(x,indexOf)结束函数Dim list2Keys = New Hashset(Of String)(File.ReadLines(TEST2).Select(keyParser))

如果TEST1中没有重复键:

Dim list1 = File.ReadLines(TEST1).ToDictionary(keyParser)Dim notFound = list1.Where(Function(kvp) Not list2Keys.Contains(kvp.Key)).Select(Function(kvp) kvp.Value)txtResults.Text = $"ID 不在列表中:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果TEST1中有重复的键,但它们并不重要:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).Select(Function(grp) grp.First)txtResults.Text = $"ID 不在列表中:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果 TEST1 中的重复键很重要:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).SelectMany(Function(grp) grp)txtResults.Text = $"ID 不在列表中:{vbCrLf}{String.Join(vbCrLf, notFound)}"

I have to lists with a number of variants. sample of first list

List 1)

hkdhksa
OP-ID: 111112
jklfjdlkfsd
hfldhfjksdf
OP-ID: 111113
ghjg
OP-ID: 111114
OP-ID: 111115
gjgjhghgjhg
OP-ID: 111116
OP-ID: 111117
OP-ID: 111118

List 2)

OP-ID: 111112
OP-ID: 11113
OP-ID: 111114
OP-ID: 111115
OP-ID: 111117

Result would be: OP-ID: 11118 is not in List 2

Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    'Declare two dictionaries. The key for each will be 
    ' the text from the input line up to,
    'but not including the first ",". 
    ' The valus for each will be the entire input line.

    'Dim file1 As New HashSet(Of String) '!
    Dim file1 As New Dictionary(Of String, String)
    Dim file2 As New Dictionary(Of String, String)

    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        Dim part() As String = line.Split(",")

        If line = ("OP-ID: ") Like "OP-ID:*" Then
            If Not file1.ContainsKey(part(0)) Then file1.Add(part(0), line)
        End If

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",")
        If Not file2.ContainsKey(part(0)) Then file2.Add(part(0), line) '!
    Next


    Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys)
    Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
    Dim str = String.Join(vbCrLf, values)

    txtResults.Text = ("IDs should not be in list: " & str)

End Sub

解决方案

Your sample lists don't have any commas, and it's not clear from the question whether they exist or not.

If there are no commas:

If there are no duplicates in TEST1 (or you don't care about them):

Dim hs As New Hashset(Of String)(File.ReadLines(TEST1))
hs.ExceptWith(File.ReadLines(TEST2))
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, hs)}"

If the duplicates are important:

Dim hs As New Hashset(Of String)(File.ReadLines(TEST2))
Dim notFound = File.ReadLines(TEST1).Where(Function(x) Not hs.Contains(x)).ToList
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"


If there are commas

(Shared code)

Dim keyParser = Function(x As String)
        Dim indexOf = s.IndexOf(
        If indexOf = -1 Then Return x
        Return Left(x, indexOf)
    End Function
Dim list2Keys = New Hashset(Of String)(File.ReadLines(TEST2).Select(keyParser))

If there are no duplicate keys in TEST1:

Dim list1 = File.ReadLines(TEST1).ToDictionary(keyParser)
Dim notFound = list1.Where(Function(kvp) Not list2Keys.Contains(kvp.Key)).Select(Function(kvp) kvp.Value)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

If there are duplicate keys in TEST1, but they don't matter:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).Select(Function(grp) grp.First)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

If the duplicate keys in TEST1 do matter:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).SelectMany(Function(grp) grp)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

这篇关于在文本文件中搜索特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆