如何让Python从文本文件中读取和提取单词?

时间:2016-12-02 12:09:25

标签: python

所以我需要创建一个打开txt文件的代码,然后获取该文件的内容并将其放入另一个txt文件,问题是,我不知道如何从中提取信息该文件,我做了一些研究,发现这是最接近的事情,但它不是我需要的:How do I get python to read only every other line from a file that contains a poem

到目前为止,这是我的代码:

myFile = open("Input.txt","wt")
myFile.close()
myFile = open("Output.txt","wt")
myFile.close()

2 个答案:

答案 0 :(得分:3)

将文本从一个文件复制到另一个文件的示例代码。也许它会对你有所帮助:

inputFile = open("Input.txt","r")
text = inputFile.read()
inputFile.close()
outputFile = open("Output.txt","w")
outputFile.write(text)
outputFile.close()

答案 1 :(得分:1)

简单地试试这个

#open input file and read all lines and save it in a list
fin = open("Input.txt","r")
f = fin.readlines()
fin.close()

#open output file and write all lines in it
fout = open("Output.txt","wt")
for i in f:
    fout.write(i)
fout.close()