-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework2-week10.py
33 lines (17 loc) · 1.01 KB
/
homework2-week10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import re
wikifile = open('wiki-file.txt') #opens file
sentences = wikifile.readlines()#reads file line by line
RegExp=re.compile("\([^|]+?)\s*\|\s*(.*)\]")# matches and group the two parts of the pattern
out_file=open("new-wiki.txt","w") # creates a new file
text="" # receives the text not matched by RE
for match in sentences: #loops trough the lines of the text
a=RegExp.search(match) #searches for the patterns
if a==None: #establishes a condition for text not matched by pattern
text=match # attach the rest of the text in an empty string
else: #otherwise
url=a.group(2) # store second part of RE
original=a.group(1) # store first part of RE
inverted_text="[" + url + " | " + original + "]" # invert the parts
text=RegExp.sub(inverted_text, match) # substitutes by the lines
out_file.write(text) # write everything into a new file
out_file.close() # close the new file