Python 即使文件中有值,对文件的搜索仍返回“无”

Python 即使文件中有值,对文件的搜索仍返回“无”,python,Python,获取了一段代码,该代码应该搜索一个文件,以查看其中是否包含搜索阶段,然后返回分配给它的数据,但是它始终不返回任何数据,即使它在文件中,我也不明白它为什么会失败 r Goblin500 IspSUjBIQ/LJ0k18VbKIO6mS1oo gorgBf6uW8d6we7ARt8aA6kgiV4 2014-08-12 06:11:58 82.26.108.68 9001 9030 s Fast HSDir Running V2Dir Valid v Tor 0.2.4.23 w Bandwidth

获取了一段代码,该代码应该搜索一个文件,以查看其中是否包含搜索阶段,然后返回分配给它的数据,但是它始终不返回任何数据,即使它在文件中,我也不明白它为什么会失败

r Goblin500 IspSUjBIQ/LJ0k18VbKIO6mS1oo gorgBf6uW8d6we7ARt8aA6kgiV4 2014-08-12 06:11:58 82.26.108.68 9001 9030
s Fast HSDir Running V2Dir Valid
v Tor 0.2.4.23
w Bandwidth=21
p reject 1-65535
是我想读的代码行

这就是我试图找到价值的方式:

def getRouter(nm):
    for r in router.itervalues():
        if r['nick'] == nm:
            return r
    return None

这就是文件内容是如何将共识解析为dict的:

# Parse the consensus into a dict
    for l in consensus_txt.splitlines():
        q = l.strip().split(" ")
        if q[0] == 'r': #router descriptor
            rfmt = ['nick', 'identity', 'digest', 'pubdate', 'pubtime', 'ip', 'orport', 'dirport']
            data = dict(zip(rfmt, q[1:]))
            idt= data['identity']
            idt += "=" * (4-len(idt)%4) # pad b64 string
            ident = data['identity'] = base64.standard_b64decode(idt)
            data['identityhash'] = binascii.hexlify(ident)
            data['identityb32'] = base64.b32encode(ident).lower()
            router[ident] = data
            curRouter = ident
        if q[0] == 's': #flags description - add to tally totals too
            router[curRouter]['flags'] = q[1:]
            for w in q[1:]:
                if flags.has_key(w):
                    flags[w]+=1
                else:
                    flags[w] = 1
            total += 1
        if q[0] == 'v':
            router[curRouter]['version'] = ' '.join(q[1:])
我错过了什么


谢谢

您的原始字符串解析有错误。这将阻止您以后匹配这些值:


基本上,zip语句的q[1:]部分只捕获字符串的第一个字符。我想你想要的是q.split[1:]。这将在空格上拆分字符串,将其转换为列表,然后忽略第一个元素。

您实际想做的是什么,代码中的变量似乎不存在?试图返回与输入昵称关联的所有数据作为全局变量,它会将s的值赋给它,例如运行V2Dir Valid的s Fast HSDir
# Parse the consensus into a dict
    for l in consensus_txt.splitlines():
        q = l.strip().split(" ")
        if q[0] == 'r': #router descriptor
            rfmt = ['nick', 'identity', 'digest', 'pubdate', 'pubtime', 'ip', 'orport', 'dirport']
            data = dict(zip(rfmt, q[1:]))
            idt= data['identity']
            idt += "=" * (4-len(idt)%4) # pad b64 string
            ident = data['identity'] = base64.standard_b64decode(idt)
            data['identityhash'] = binascii.hexlify(ident)
            data['identityb32'] = base64.b32encode(ident).lower()
            router[ident] = data
            curRouter = ident
        if q[0] == 's': #flags description - add to tally totals too
            router[curRouter]['flags'] = q[1:]
            for w in q[1:]:
                if flags.has_key(w):
                    flags[w]+=1
                else:
                    flags[w] = 1
            total += 1
        if q[0] == 'v':
            router[curRouter]['version'] = ' '.join(q[1:])
q = 'r Goblin500 IspSUjBIQ/LJ0k18VbKIO6mS1oo gorgBf6uW8d6we7ARt8aA6kgiV4 2014-08-12 06:11:58 82.26.108.68 9001 9030'
rfmt = ['nick', 'identity', 'digest', 'pubdate', 'pubtime', 'ip', 'orport', 'dirport']
data = dict(zip(rfmt, q[1:]))

print(data)
# {'pubdate': 'b', 'dirport': '5', 'ip': 'i', 'orport': 'n', 'nick': ' ', 'identity': 'G', 'digest': 'o', 'pubtime': 'l'}

print(data['nick'])
# prints out a single space