首页 > 解决方案 > 在 .txt 文件中包含值的循环内循环

问题描述

这是我的代码:

filenames = []
data = []
found = False

axconfig = open('axcfgpasww.dat', "r")
dictionnary = open("hello.txt", "r")

output_value = "bonjour"
for line in axconfig:
    for word in dictionnary:
        element = word.split("=")[0]
        if element in axconfig:
            # old_value = line.split("=")
            # data.append(line.replace(old_value, output_value+'\n'))

            # found = True
        # else:
            # data.append(line)

            print(element)

if not found:
    print('No match found')

# Create a new file, from data array
with open("olivier.txt", 'w') as outfile:
    for line in data:
        outfile.write(line)

动态值为“元素”。

我有一个 .txt 文件,它有关键字,并且想用 .txt 文件中的值查看“元素”。

但是我的代码不起作用..它只在 olivier.txt 中添加 axcfgpasww.dat 的第一行..并且还返回“未找到匹配项”

多谢你们 !

编辑 :

axcfgpasww.dat

T72_BANK_IDENTIFIER_CODE_3=SOAPFR22
T72_ISSUER_COUNTRY_CODE_3=FR
T72_MERCHANT_ID_3=
T72_VAS_SCHEME_IDENTIFIER_3=

你好.txt

T72_VAS_SERVICE_IDENTIFIER_7=
T72_VAS_SCHEME_IDENTIFIER_3=

标签: python

解决方案


为方便起见,如果我将两者都视为文本文件。

下面的代码会将 axconfig 的每一行与 hello 匹配,然后打印匹配的内容,并将该值存储为一个名为“value”的变量

filenames = []
data = []
found = False
value = ""
axconfig =  open('axcfgpasww.txt', "r")
lines = axconfig.readlines()
dictionnary = open("hello.txt", "r")

output_value = "bonjour"
for line in lines:
    match = line.split("=")[0]
    for word in dictionnary:
        element = word.split("=")[0]   
    if element == match:
        print(element)
        value = element

推荐阅读