繁体   English   中英

如何使用 python 在文件中写入我的构建参数作为键和值

[英]How to write my build parameter in file as key and value using python

我有一个带有构建参数“WorkspaceName”的 Jenkins 构建,我有一个 python 脚本,它打印 WorkspaceName,它必须将工作空间名称写入像 WorkspaceName="The value provided in Z2E54334C0A5CE23E3E5A584ZDFAB3" 这样的文件。

 import time
 import os
 wsname = os.getenv("WorkspaceName")
 print (wsname)
 fo = open("MyParameters.properties", "w+")
 fo.write(wsname)
 fo.close()

如果我使用字典,它会在这样的文件中打印{1: 'aaa'}

但我需要 key=value,(WorkspaceName=MyWorkspaceName)。 我是 python 的新手。

这就是dict默认实现打印的方式。 如果你想要它不同,你必须拉出你想要的部分并相应地格式化。 您可以使用dict.items()方法迭代键和值:

with open('MyParameters.properties', 'w+') as fo:
    for key, val in wsname.items():
        new_line = f'{key}="{val}"'
        # Or using format -> '{0}="{1}"'.format(key, val)

        # Writes to open file, adds a newline at the end
        print(new_line, file=fo)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM