python输出列表元素_在python中将列表元素打印到单独的文本文件中

1586010002-jmsa.png

Im new to python programming and need some help with some basic file I/O and list manipulation.

currently i have a list (s) that has these elements in it:

['taylor343', 'steven435', 'roger101\n']

what i need to do is print each line into new text files with only the 1 element in the text files as shown below:

file1.txt

taylor343

file2.txt

steven435

file3.txt

roger101

Im currently trying to work with this using a loop but i can only output into 1 text file

for x in list:

output.write(x+"\n")

How can i get it to write every single line of list into new text files (not just one)

Thank you

解决方案

You need to open each new file you want to write into. As a quick example:

items = ['taylor', 'steven', 'roger']

filenames = ['file1', 'file2', 'file3']

for item, filename in zip(items, filenames):

with open(filename, 'w') as output:

output.write(item + '\n')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值