使用Python将列表写入文本文件 [英] Writing a list to a text file using Python

查看:249
本文介绍了使用Python将列表写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这种格式的列表,



['com','kill','someone','get','caught' ]



我需要将其写入文本文件,



com杀人被抓住



基本上我需要单词之间的空格。



我尝试了什么:



但是,我尝试了一些方法,例如,



1)

I have a list in this format,

['com', 'kill', 'someone', 'get', 'caught']

And i need this to be written on to a text file as,

com kill someone get caught

Basically i need spaces between words.

What I have tried:

However, i have tried methods such as,

1)

for t in some_list:
  f.write(' '.join(str(s) for s in t) + '\n')



2)


2)

" ".join(map(str, f))





其中f是列表



以上情况似乎都不能得到我的结果。任何帮助?



Where f is the list

Non of the above seem to work to get my outcome. Any help?

推荐答案

你根本不需要for循环

试试这个

some_list = [' com','kill','someone','get','caught']

f.write(''。join(str(s)for s in some_list))
you dont need the for loop at all
try this
some_list = ['com', 'kill', 'someone', 'get', 'caught']
f.write(' '.join(str(s) for s in some_list))


这篇关于使用Python将列表写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆