[英]Python- Joining the list items in a text file
您应该使用ast.literal_eval
解析文件内容, ast.literal_eval
将其转换为实际列表,然后使用str.join
方法将列表项合并为一个字符串:
from ast import literal_eval
with open("Result.txt","r+",encoding="utf8") as file, open('output.txt', 'w') as output:
output.write(' '.join(literal_eval(file.read())))
您会得到一个字符串。
用
text = text[1:-1]
然后删除“'”和“,”
text.replace("'", "").replace(",","")
您也可以用相同的方法删除'\\\\ n'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.