【问题标题】:How to get all the words of the doc2 in the doc1?如何获取doc1中doc2的所有单词?
【发布时间】:2017-03-15 17:29:02
【问题描述】:

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

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

【问题讨论】:

  • 显示您尝试过的内容并定义“单词”。
  • 你试过什么?我建议您阅读该问题的答案:stackoverflow.com/questions/7409780/…
  • 如何获取doc1中doc2的所有单词?如果我没有完成我的问题,我很抱歉。
  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?
  • 对不起,我不知道。

标签: python regex


【解决方案1】:

如果要将 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)

希望对你有帮助,对我的英语不好表示抱歉。

【讨论】:

  • 感谢您的回答。但我需要在 doc 2 中找到 doc1 单词,反之亦然。
  • @GilangPratama 对不起,我不明白你的问题。您想要:将 doc1 的文本复制到 doc2 或将 doc2 复制到 doc1 或者您想要比较它们并打印其差异?。
【解决方案2】:

您的问题根本不清楚。我只是在这里猜测:

您想读取 doc1 的所有内容并将其写入 doc2。

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

【讨论】:

  • 感谢您的回答。但我需要在 doc 2 中找到 doc1 单词,反之亦然。
【解决方案3】:

您是否真的尝试理解您的代码。您应该先以写入模式打开目标文件才能进行写入。

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

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

并且在您的第四行代码中,您正在使用读取功能,您应该使用写入功能,因为您必须将 doc1 的内容复制到它 如下所示。

file_doc1.write(isi_stop)

在这里,您可以将您从 doc1 中读取的内容放入 doc2。

【讨论】:

  • 感谢您的回答。但我需要在 doc 2 中找到 doc1 单词,反之亦然。
  • 亲爱的 Gilang 尝试用提到的编辑替换你的代码,你会发现它有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多