我有一个名为ClientList.txt
的文件,其输出为:
client1.hello.com
client2.hello.com
client3.hello.com
我想使用python将这些行一个接一个地添加到我的另一个名为output.txt
的文件中。
我想在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的新手,所以我不确定如何接受这个。
答案 0 :(得分:0)
你可以在Python中这样做:
tr -d '\r' < fileWithWindowsLineEndings | grep -E '...'
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
应该这样做。