尝试打印与一个键关联的所有值,例如:
按马库斯搜索,我想返回5行。
词典:
file.FullName = "/factor/dktest/pdi/gl_accounts.txt"
StackTrace
at Renci.SshNet.Common.AsyncResult.EndInvoke()
at Renci.SshNet.Sftp.SftpSession.EndOpen(SftpOpenAsyncResult asyncResult)
at Renci.SshNet.ServiceFactory.CreateSftpFileReader(String fileName, ISftpSession sftpSession, UInt32 bufferSize)
at Renci.SshNet.SftpClient.InternalDownloadFile(String path, Stream output, SftpDownloadAsyncResult asyncResult, Action`1 downloadCallback)
at Renci.SshNet.SftpClient.DownloadFile(String path, Stream output, Action`1 downloadCallback)
at FtpTesting.frmSshNet.button7_Click(Object sender, EventArgs e) in C:\\Development\\FtpTesting\\FtpTesting\\frmSshNet.cs:line 98"
我不知道这是否是最简单的方法,但只会返回最后一行。
marcus {'TIME': 'Mar 3 15:49:33', 'CONNECTION': 'logout'}
fabin {'TIME': 'Mar 3 00:20:16', 'CONNECTION': 'login'}
marcus {'TIME': 'Mar 3 15:49:29', 'CONNECTION': 'logout'}
peter {'TIME': 'Mar 3 13:36:16', 'CONNECTION': 'logout'}
marcus {'TIME': 'Mar 3 14:28:56', 'CONNECTION': 'logout'}
peter {'TIME': 'Mar 3 02:17:34', 'CONNECTION': 'logout'}
marcus {'TIME': 'Mar 3 18:12:50', 'CONNECTION': 'logout'}
fabin {'TIME': 'Mar 3 22:50:15', 'CONNECTION': 'logout'}
peter {'TIME': 'Mar 3 15:14:16', 'CONNECTION': 'logout'}
marcus {'TIME': 'Mar 3 08:19:17', 'CONNECTION': 'login'}
peter {'TIME': 'Mar 3 15:16:45', 'CONNECTION': 'logout'}
fabin {'TIME': 'Mar 3 15:30:10', 'CONNECTION': 'logout'}
carl {'TIME': 'Mar 3 15:49:33', 'CONNECTION': 'logout'}
答案 0 :(得分:2)
我怀疑您一直在向字典添加项目,并且字典只能具有唯一键。考虑:
D = {'marcus':{'TIME': 'Mar 3 15:49:33', 'CONNECTION': 'logout'},
'fabin':{'TIME': 'Mar 3 00:20:16', 'CONNECTION': 'login'},
'marcus':{'TIME': 'Mar 3 15:49:29', 'CONNECTION': 'logout'},
'peter':{'TIME': 'Mar 3 13:36:16', 'CONNECTION': 'logout'},
'marcus':{'TIME': 'Mar 3 14:28:56', 'CONNECTION': 'logout'},
'peter':{'TIME': 'Mar 3 02:17:34', 'CONNECTION': 'logout'},
'marcus':{'TIME': 'Mar 3 18:12:50', 'CONNECTION': 'logout'},
'fabin':{'TIME': 'Mar 3 22:50:15', 'CONNECTION': 'logout'},
'peter':{'TIME': 'Mar 3 15:14:16', 'CONNECTION': 'logout'},
'marcus':{'TIME': 'Mar 3 08:19:17', 'CONNECTION': 'login'},
'peter':{'TIME': 'Mar 3 15:16:45', 'CONNECTION': 'logout'},
'fabin':{'TIME': 'Mar 3 15:30:10', 'CONNECTION': 'logout'},
'carl':{'TIME': 'Mar 3 15:49:33', 'CONNECTION': 'logout'}}
for k,v in D.items():
print(k,v)
if k == 'marcus':
print(v)
输出(匹配描述的行为):
{'TIME': 'Mar 3 08:19:17', 'CONNECTION': 'login'}
尽管看起来有多个,但是字典只会保留唯一键的最后一个条目。如果需要具有相同名称的多个实例,则需要不同的结构。代替(可疑的)命令,使用命令列表:
L = [{'NAME':'marcus','TIME':'Mar 3 15:49:33','CONNECTION':'logout'},
{'NAME':'fabin','TIME':'Mar 3 00:20:16','CONNECTION':'login'},
{'NAME':'marcus','TIME':'Mar 3 15:49:29','CONNECTION':'logout'},
{'NAME':'peter','TIME':'Mar 3 13:36:16','CONNECTION':'logout'},
{'NAME':'marcus','TIME':'Mar 3 14:28:56','CONNECTION':'logout'},
{'NAME':'peter','TIME':'Mar 3 02:17:34','CONNECTION':'logout'},
{'NAME':'marcus','TIME':'Mar 3 18:12:50','CONNECTION':'logout'},
{'NAME':'fabin','TIME':'Mar 3 22:50:15','CONNECTION':'logout'},
{'NAME':'peter','TIME':'Mar 3 15:14:16','CONNECTION':'logout'},
{'NAME':'marcus','TIME':'Mar 3 08:19:17','CONNECTION':'login'},
{'NAME':'peter','TIME':'Mar 3 15:16:45','CONNECTION':'logout'},
{'NAME':'fabin','TIME':'Mar 3 15:30:10','CONNECTION':'logout'},
{'NAME':'carl','TIME':'Mar 3 15:49:33','CONNECTION':'logout'}]
for d in L:
if d['NAME'] == 'marcus':
print(d)
输出:
{'NAME': 'marcus', 'TIME': 'Mar 3 15:49:33', 'CONNECTION': 'logout'}
{'NAME': 'marcus', 'TIME': 'Mar 3 15:49:29', 'CONNECTION': 'logout'}
{'NAME': 'marcus', 'TIME': 'Mar 3 14:28:56', 'CONNECTION': 'logout'}
{'NAME': 'marcus', 'TIME': 'Mar 3 18:12:50', 'CONNECTION': 'logout'}
{'NAME': 'marcus', 'TIME': 'Mar 3 08:19:17', 'CONNECTION': 'login'}
另一个选项是列表的字典,将每个infodict附加到用户密钥的值中:
D = {'marcus':[{'TIME':'Mar 3 15:49:33','CONNECTION':'logout'},
{'TIME':'Mar 3 15:49:29','CONNECTION':'logout'},
{'TIME':'Mar 3 14:28:56','CONNECTION':'logout'},
{'TIME':'Mar 3 18:12:50','CONNECTION':'logout'},
{'TIME':'Mar 3 08:19:17','CONNECTION':'login'}],
'fabin':[{'TIME':'Mar 3 00:20:16','CONNECTION':'login'},
{'TIME':'Mar 3 22:50:15','CONNECTION':'logout'},
{'TIME':'Mar 3 15:30:10','CONNECTION':'logout'}],
'peter':[{'TIME':'Mar 3 13:36:16','CONNECTION':'logout'},
{'TIME':'Mar 3 02:17:34','CONNECTION':'logout'},
{'TIME':'Mar 3 15:14:16','CONNECTION':'logout'},
{'TIME':'Mar 3 15:16:45','CONNECTION':'logout'}],
'carl':[{'TIME':'Mar 3 15:49:33','CONNECTION':'logout'}]}
for item in D['marcus']:
print(item)
输出:
{'TIME': 'Mar 3 15:49:33', 'CONNECTION': 'logout'}
{'TIME': 'Mar 3 15:49:29', 'CONNECTION': 'logout'}
{'TIME': 'Mar 3 14:28:56', 'CONNECTION': 'logout'}
{'TIME': 'Mar 3 18:12:50', 'CONNECTION': 'logout'}
{'TIME': 'Mar 3 08:19:17', 'CONNECTION': 'login'}