在循环内循环,其中包含.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 axcfgpaswww.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=

问题出在以下行上:

if element in axconfig:

请尝试:

if element in line.split("=")

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

下面的代码将 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

最新更新