从字典中合并多个字符串并使用python保存在文件中

时间:2019-01-18 05:13:30

标签: python string dictionary

我能够在/tmp/filter.log中写入主机名,但是任何提示如何将所有三个值[主机名,所有者,席位]写入文件中?

def list_hosts(nc):
    resp = nc.send_service_request('ListHosts', json.dumps({}))
    result = resp['result']
    l = []
    f=open("/tmp/filter.log", "w+")
    for r in result:
        if "team-prod" in r['owner']:
            print r['owner'], r['hostname'], r['seats']
            f.write(r['hostname'] + "\n")
    f.close()
    l.append(r['hostname']) 
    return l
nc = create_client('zone', 'team_PROD_USERNAME', 'team_PROD_PASSWORD')
l = list_hosts(nc)
print l

该文件应具有以下条目:

team-prod\*, np-team-052, [u'123123123-18d1-483d-9af8-169ac66b26e4']

当前条目是:

np-team-052

1 个答案:

答案 0 :(得分:0)

f.write(str(r ['owner'])+','+ str(r ['hostname'])+','+ str(r ['seats'])+'\ n')