使用列表在textfilepython中搜索

2024-05-18 20:14:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,但我相信您应该在实践中学习。所以这里是:

我试图创建一个小的CLI应用程序,它以两个文本文件作为输入。 然后应该从该文件创建一个公共ssid列表SSID.txt文件,然后通过Kismet.netxt文件以查看有多少访问点具有公用名称。在

我走对了吗?这就是我目前所拥有的,它进口SSID.txt文件到一个名为“ssids”的变量中

f = open('SSID.txt', "r")
s = open('Kismet.nettxt', "r")


for line in f:
    ssids = line.strip()



s.close()
f.close()

有什么关于我应该从这里开始的建议吗?在

文件的格式如下:

在SSID.txt文件公司名称:

^{pr2}$

这就是Kismet.netxt文件格式为:

Network 3: BSSID REMOVED
 Manuf      : Siemens
 First      : Sun Dec 29 20:59:46 2013
 Last       : Sun Dec 29 20:59:46 2013
 Type       : infrastructure
 BSSID      : REMOVED
   SSID 1
    Type       : Beacon
    SSID       : "Internet" 
    First      : Sun Dec 29 20:59:46 2013
    Last       : Sun Dec 29 20:59:46 2013
    Max Rate   : 54.0
    Beacon     : 10
    Packets    : 2
    Encryption : WPA+PSK
    Encryption : WPA+TKIP
 Channel    : 5
 Frequency  : 2432 - 2 packets, 100.00%
 Max Seen   : 1000
 LLC        : 2
 Data       : 0
 Crypt      : 0
 Fragments  : 0
 Retries    : 0
 Total      : 2
 Datasize   : 0
 Last BSSTS : 
    Seen By : wlan0 (wlan0mon)

Tags: 文件txt名称closelineopendecsun
2条回答

此代码应满足您在OP中要求的所有操作:

try:
    with open('SSID.txt', 'r') as s:
        ssid_dict = {}
        for each_line in s:
            ssid_dict[each_line.strip()] = 0 #key: SSID value: count
except FileNotFoundError:
    pass

try:
    with open('kissmet.nettext', 'r') as f:
        try:
            for each_line in f:
                each_line = each_line.strip()
                if each_line.startswith("SSID") and ':' in each_line: #checks for a line that starts with 'SSID' and contains a ':'
                    val = each_line.split(':')[1].replace('"', '').strip() #splits the line to get the SSID, removes the quotes
                    if ssid_dict[val]:
                        ssid_dict[val] += 1 #adds one to the count in the dictionary
                    else:
                        pass#I don't know what you want to do here
        except KeyError as err:
            print("Key error" + str(err))
except FileNotFoundError:
    pass

for key in ssid_dict:
    print(str(key) + " " + str(ssid_dict[key]))

it输出:

^{pr2}$

出于测试目的,我将“Internet”添加到SSID的列表中。在

编辑:我已经更新了添加到count的部分,以处理字典中没有的键。我不知道你想怎么处理那些不是这样的,我在里面留了一个pass

这里有一些我应该如何做的建议。在

  1. 阅读SSID.txt文件,创建名称的dict,这样您就可以快速查找每个名称并存储计数。如果在SSID.txt文件文件。在
  2. 阅读Kismet.netxt文件,如果行以has“SSID:”开头,则取dict中的名称和查找,如果找到则添加到计数中。在
  3. 此时,您将拥有一个具有名称和计数的ssids字典。在

代码如下所示:

f = open('SSID.txt', "r")
s = open('Kismet.nettxt', "r")

ssids = {}  # Create dictionary

for line in f:
    # Add each to the dictionary, 
    # if there are duplicates this effectively removes them
    ssids[line.strip()] = 0

for line in s:

    # Check the lines in the kismet file that start with the SSID we are after
    if line.strip().startswith('SSID       :'):

        # Break the entry at : and take the second part which is the name
        kismet = line.split(':')[1].strip()

        # Remove the " marks from front and back and lookup in the ssids
        # add to the count if you find it.
        if kismet[1:-1] in ssids:
            ssids[kismet[1:-1]] += 1

s.close()
f.close()

相关问题 更多 >

    热门问题