added regex_search
This commit is contained in:
1
out_mad_libs.txt
Normal file
1
out_mad_libs.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
The silly panda walked to the chandelier and then screamed. A nearby picked truck was unaffected by these events.
|
||||||
@@ -31,9 +31,15 @@ mad_libs.py - reads in text files and lets the user add their own text anywhere
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
file_name = input("Enter the filename:")
|
in_file_name = input("Enter the filename:\n")
|
||||||
file_read = open(file_name)
|
in_file = open(in_file_name)
|
||||||
content = file_read.read()
|
content = in_file.read()
|
||||||
|
|
||||||
for to_replace in re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB').findall(content):
|
for to_replace in re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB').findall(content):
|
||||||
content.replace(to_replace)
|
str_input = input("Enter %s %s:\n" % ('an' if to_replace[0] in 'AEIOU' else
|
||||||
|
'a', to_replace.lower()))
|
||||||
|
content = content.replace(to_replace, str_input, 1)
|
||||||
|
|
||||||
|
out_file_name = input("Enter the output file name:\n")
|
||||||
|
out_file = open(out_file_name, 'w')
|
||||||
|
out_file.write(content)
|
||||||
|
|||||||
14
practice_projects/regex_search.py
Normal file
14
practice_projects/regex_search.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#! python3
|
||||||
|
'''
|
||||||
|
regex_search.py - opens all .txt files in a folder and searches for any line
|
||||||
|
that matches a user-supplied regular expression. The results
|
||||||
|
should be printed to the screen.
|
||||||
|
'''
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
target_folder = input("Enter the target foulder:\n")
|
||||||
|
os.chdir(target_folder)
|
||||||
|
target_files = os.listdir(target_folder)
|
||||||
|
target_files = [target for target in target_files if target.endswith('.txt')]
|
||||||
|
print(target_files)
|
||||||
Reference in New Issue
Block a user