簡體   English   中英

如何在 Python 中僅讀取文本文件中的文本?

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

我有一個包含一些數據的文本,我需要將其復制到另一個文本文件中 如何讀取文本並將其復制到我的新文本文件中?

文字看起來像這樣

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

我只需要讀取文本(沒有列表)並將其分配給變量以將它們復制到新的文本文件中

我試過了

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

為了只獲取文本文件中的列表,但現在我只需要文本文件。

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

希望這有幫助。 =)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM