如何从文件加载数据并循环使用它们?

时间:2019-06-18 11:35:23

标签: python json python-3.x loops jsonparser

具有将以下内容输出到txt文件的python代码:

out.txt包含:

"1":"pass1425-*",
"2":"pass1234- ",
"3":"pass0*++",
........

 .....

"100":"pass*-+"

需要将此1,2,3,4,5,6,... 100的值用于循环功能

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

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

1应该返回pass1425- *

2应该返回pass1234-

在提到id的循环中 ... ...

1 个答案:

答案 0 :(得分:0)

我从您的问题中了解了什么

我在下面写了代码

f = open("out.txt","r")

dictres = {}

    for line in f:
        try:
            val = line.split(":")
            dictres[int(val[0].replace('"',""))] = val[1].replace('"',"").replace(",","").replace("\n","")
        except Exception as e:
            print("Data in  line is not enough to format")

    ids = (1,2,3)
    for id in ids :
        try:
            print(dictres[1])
        except:
            print(str(id)+"id not found ")