我有一个名为的文件ClientList.txt
,其输出为:
client1.hello.com
client2.hello.com
client3.hello.com
我想将这些行和附加文本一一附加到我output.txt
使用 python调用的另一个文件中。
我想在我的output.txt
文件中实现的示例:
clients name: client1.hello.com, clients URL: client1.hello.com, service: VIP
clients name: client2.hello.com, clients URL: client2.hello.com, service: VIP
clients name: client3.hello.com, clients URL: client3.hello.com, service: VIP
有人可以帮助我实现这一目标吗?
到目前为止我尝试过的:
def main():
f= open("output.txt","w+")
for i in range(3):
f.write("clients name: client1.hello.com, clients URL: client1d.hello.com, service: VIP")
f.close()
我得到的输出:
clients name: client1.hello.com, clients URL: client1d.hello.com, service: VIPclients name: client1.hello.com, clients URL: client1d.hello.com, service: VIPclients name: client1.hello.com, clients URL: client1d.hello.com, service: VIP
我是python的新手,所以我不确定如何解决这个问题。
你可以在 Python 中这样做:
with open("ClientList.txt", "r") as infile:
with open("output.txt", "w") as outfile:
for line in infile:
outfile.write("".join(["clients name: ",line.strip(), ", clients URL: ", line.strip(), ", service: VIP\n"]))
内容output.txt
:
clients name: client1.hello.com, clients URL: client1.hello.com, service: VIP
clients name: client2.hello.com, clients URL: client2.hello.com, service: VIP
clients name: client3.hello.com, clients URL: client3.hello.com, service: VIP
同样,看到您编辑了您的帖子,为了让您的代码正常工作,您应该将write
行更改为以下内容:
f.write("clients name: client1.hello.com, clients URL: client%d.hello.com, service: VIP\n" % (i+1))
那应该做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句