Hello, I have the code below that finds a person(LookFor=) from a dialogue file and creates for him a new file with his name and the words he have said. Is it possible to automate this and create a file for every person that is in the dialogue file instead of writing one name at a time?
I am thinking to have another file with the list of names and somehow to check it with the dialogue files but I do not know how to do it. Any help please?
Rich Smith: I'm going to attend a concert on Saturday.
Do you have any special plans?
Peter Aderson (the boss) : No, I'm going to relax. What did you do last weekend?
Rich Smith: Last weekend, I went to visit my friends in San Francisco. What did you do?
Peter Aderson (the boss): I played soccer with some friends.
Mary Sarah: Hello
John Daisa: hi
The output file RichSmith.txt
Rich.txt
Rich Smith: I'm going to attend a concert on Saturday
Rich Smith: Last weekend, I went to visit my friends in San Francisco. What did you do?
PeterAderson.txt
Peter Aderson(the boss): No, I'm going to relax. What did you do last weekend?
Peter Aderson (the boss): I played soccer with some friends.
MarySarah.txt
Mary Sarah: Hello
JohnDaisa.txt
John Daisa: hi
I am thinking to have another file with the list of names and somehow to check it with the dialogue files but I do not know how to do it. Any help please?
import os,re lines = [] exportedLines = [] LookFor = 'Rich Smith' excludedNames = re.compile(r'^(?!:)|(:| :)?') with open('sample.txt') as dialogue: lines = dialogue.readlines() for line in lines: mo = excludedNames.search(line) if line.startswith(LookFor): exportedLines += [line] elif line.startswith(LookFor) == False and lines[lines.index(line)-1].startswith(LookFor)== True and mo == None: exportedLines += [line] with open('./%sWords.txt' % (LookFor),'w') as publish : publish.write("".join(exportedLines))The input file sample.txt contains this:
Rich Smith: I'm going to attend a concert on Saturday.
Do you have any special plans?
Peter Aderson (the boss) : No, I'm going to relax. What did you do last weekend?
Rich Smith: Last weekend, I went to visit my friends in San Francisco. What did you do?
Peter Aderson (the boss): I played soccer with some friends.
Mary Sarah: Hello
John Daisa: hi
The output file RichSmith.txt
Output:Rich Smith: I'm going to attend a concert on Saturday.
Rich Smith: Last weekend, I went to visit my friends in San Francisco. What did you do?
I need to have for output:Rich.txt
Rich Smith: I'm going to attend a concert on Saturday
Rich Smith: Last weekend, I went to visit my friends in San Francisco. What did you do?
PeterAderson.txt
Peter Aderson(the boss): No, I'm going to relax. What did you do last weekend?
Peter Aderson (the boss): I played soccer with some friends.
MarySarah.txt
Mary Sarah: Hello
JohnDaisa.txt
John Daisa: hi