【问题标题】:Appending words from a file to other different files将文件中的单词附加到其他不同的文件
【发布时间】:2020-06-06 09:16:41
【问题描述】:

我有一个文本文件,其中的内容是不同人之间的对话。例如-

WILL: Hii , 
ROYCE: Hello , 
WILL: How r u? , 
ROYCE: Fine , 
WILL: Where have h been?

我想为每个带有名字的字符创建一个新的文本文件,并将他们所说的唯一单词存储在各自的文件中。如何使用 python 做到这一点?

【问题讨论】:

  • 请用您尝试过的代码更新您的问题。

标签: python


【解决方案1】:

希望这会有所帮助。为此,我们必须先创建两个单独的文件。

with open('Will.txt', 'r+a') as newfile_1,open('Royce.txt', 'r+a') as newfile_2:
     Will_lines_lst = newfile_1.readlines()
     Royce_lines_lst = newfile_2.readlines()     
     with open('name_file.txt','r+w') as f:
        for line in f:
            if line not in Will_lines_lst and line[0] == 'W':
                 newfile_1.append(line)
            elif line not in Royce_lines_lst:
                 newfile_2.append(line)   

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    相关资源
    最近更新 更多