python - 在 .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=
解决方案
为方便起见,如果我将两者都视为文本文件。
下面的代码会将 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
推荐阅读
- arrays - 使用 for 循环设置按钮的标题
- java - 使用 /usr/libexec/java_home 切换 java home 在 MacOS 升级到 Big Sur 后停止工作
- vue.js - 如何在 web.config 中使用 .env 变量?
- wpf - 如何将 IGrouping 绑定到包含 dataTemplate 的数据网格?
- dll - 用于 dot net core 应用程序的 CI/CD 管道,其中 2 个 DLL 文件存储在 Azure DevOps 存储库的文件夹中 - 构建和发布问题
- c++ - 是否可以在 C++ 中使用容器的大小作为 priority_queue 的优先级?
- ruby-on-rails - 未定义的方法“start_with?” 对于 false:FalseClass on rails 生成 -p
- kotlin - 如何防止 Kotlin 中的类型擦除?
- google-cloud-firestore - 是否有一种简单的方法可以将文档字段值复制到同一文档中的另一个新字段以用于整个 Firestore 集合
- swift - 在 Swift 中,有没有办法知道文件是否正在下载?