如何在doc1中获得doc2的所有单词?

2024-05-15 22:11:00 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在doc1中获得doc2的所有单词?你知道吗

file_stop = open('doc1','r')
isi_stop = file_stop.read()
file_doc1 = open('doc2','r')
isi_doc1 = file_doc1.read()

Tags: readdoc1open单词filestopisidoc2
3条回答

如果要将doc2的文本复制到doc1,请使用:

with open('doc2', 'r') as doc2:
    read = doc2.readlines()
    with open('doc1', 'w') as doc2:   # Not 'r' (read), use 'w' (write)
         doc1.writelines(read)

解释(如果不好,请道歉):

open(file that you want to open, mode of opening)
# open the file with an specific mode, ('r' read, 'w' write, there are other like 'r+', 'w+', etc)
# you have to use open with a variable like:
file = open('doc1', 'r')
with open('doc1', 'r') as doc:
# you open the doc1 in the variable doc for a shot while, i mean, when you finish to use the file, it will automatly close.
read = doc2.readlines()
# you read the file (in this case 'doc2') (only can be done with reading modes [or readding and writting modes]) and load it in a variable ('read')
doc1.writelines(read)
# you write in the file ('doc1') all the text or values loaded in the variable ('read'), (you can only write in writting modes (or writting and readding)

我希望这对你有帮助,也为我的英语不好感到抱歉。你知道吗

你真的试着去理解你的代码吗。您应该首先以写模式打开目标文件,以便能够进行写操作。你知道吗

所以你的第三行代码应该是。你知道吗

file_doc1=open('doc2','w')

在第四行代码中,您使用的是read函数,您应该使用write函数,因为您必须将doc1的内容复制到其中 具体如下。你知道吗

file_doc1.write(isi_stop)

在这里你将把你从doc1读到的东西放到doc2。你知道吗

你的问题一点也不清楚。我只是在猜:

您希望读取doc1的所有内容并将其写入doc2。你知道吗

with open('doc2', 'r') as doc2:
    reader = doc2.readlines()
    with open('doc1', 'a') as doc1:
        doc1.writelines(reader)

相关问题 更多 >