简体   繁体   English

如何在 Python 中仅读取文本文件中的文本?

[英]How do I read just text in a text file in Python?

I have a text with some data I need to copy into another text file How do I read the text and copy it into my new text file?我有一个包含一些数据的文本,我需要将其复制到另一个文本文件中 如何读取文本并将其复制到我的新文本文件中?

The text looks like this文字看起来像这样

 bruce
 bruce@yahoo.es
 ['sony', 1.89172]
 ['samsung', 6.0535]

I need to read only the text (without lists) and asign it to a variable to copy them onto a new text file我只需要读取文本(没有列表)并将其分配给变量以将它们复制到新的文本文件中

I've tried我试过了

with open("InformeFinal.txt") as f:
registros = [ast.literal_eval(x) for x in f if x.startswith('[')]

In order to get only the lists inside the text file, but now I need just the text file.为了只获取文本文件中的列表,但现在我只需要文本文件。

file = open('fromfile.txt')
filec = file.read()
fileb = open('tofile.txt', 'w')
fileb.write(filec)
file.close()
fileb.close()

Hope this helped.希望这有帮助。 =) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM