如何读取打印语句并在循环中使用它们?

时间:2019-06-18 09:56:06

标签: python python-3.x loops

代码:

import re
import json
#open("sbins.txt","r")  
data = open("sbiiins.txt","r")
contents = data.read().strip()



for i, p in enumerate(re.findall(r'"password":[^"]*"(.*?)"', contents):

        print('{}="{}"'.format(i, p))

此代码通过解析文本文件将以下字符串输出:

1="pass1425-*"
2="pass1234- "
3="pass0*++"
4="pass*-+"

我需要使用这些打印语句并在循环中使用它们

例如:

ids = (1,2,3,5,6,7,8)

for ind in ids:
     a= ids
      ....
       ....

在上面的代码中

1应该返回pass1425-*

2应该返回pass1234-
...

1 个答案:

答案 0 :(得分:1)

正如评论所说,使用字典。

https://www.w3schools.com/python/python_dictionaries.asp

thisdict =  {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["year"] = 2018

您的情况:

thisdict =  {
    "1":"pass1425-*",
    "2":"pass1234- ",
    "3":"pass0*++",
    "4":"pass*-+"
}
print(thisdict["2"])

>>>>pass1234-