Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e086e9f

Browse files
committed
New stuff
1 parent f0de22f commit e086e9f

19 files changed

+170
-0
lines changed

‎C8_mad_libs.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Create a Mad Libs program that reads in text files and lets the user add
3+
their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB
4+
appears in the text file. For example, a text file may look like this:
5+
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
6+
unaffected by these events.
7+
The program would find these occurrences and prompt the user to
8+
replace them.
9+
Enter an adjective:
10+
silly
11+
Enter a noun:
12+
chandelier
13+
Enter a verb:
14+
screamed
15+
Enter a noun:
16+
pickup truck
17+
The following text file would then be created:
18+
The silly panda walked to the chandelier and then screamed. A nearby pickup
19+
truck was unaffected by these events.
20+
The results should be printed to the screen and saved to a new text file.
21+
22+
"""
23+
import re
24+
25+
file = open('.\\files\\c8_text.txt', 'r')
26+
content = file.read()
27+
mad_lib_words = list(content.split())
28+
file.close()
29+
30+
adj_regex = re.compile(r'ADJECTIVE')
31+
noun_regex = re.compile(r'NOUN')
32+
adv_regex = re.compile(r'ADVERB')
33+
verb_regex = re.compile(r'VERB')
34+
35+
result_file = open('.\\files\\c8_text_result.txt', 'w')
36+
result_string = ""
37+
for word in mad_lib_words:
38+
if adj_regex.match(word):
39+
word = adj_regex.sub(input("Give an adjective: "), word)
40+
elif noun_regex.match(word):
41+
word = noun_regex.sub(input("Give a noun: "), word)
42+
elif verb_regex.match(word):
43+
word = verb_regex.sub(input("Give a verb: "), word)
44+
elif adv_regex.match(word):
45+
word = adv_regex.sub(input("Give a adverb: "), word)
46+
result_string += word + " "
47+
result_file.write(word + " ")
48+
49+
print(result_string)
50+
result_file.close()

‎C8_regex_search.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Write a program that opens all .txt files in a folder and searches for any
3+
line that matches a user-supplied regular expression. The results should
4+
be printed to the screen.
5+
6+
"""
7+
import os
8+
import re
9+
10+
dir_files = os.listdir('.\\files\\c8_dir')
11+
print('What text do you want to search for?')
12+
user_regex = re.compile(str(input()))
13+
file_regex = re.compile(r'\w+\.txt$')
14+
15+
for i in range(len(dir_files)):
16+
if file_regex.match(dir_files[i]):
17+
file = open('.\\files\\c8_dir\\' + dir_files[i])
18+
content = file.readlines()
19+
for line in range(len(content)):
20+
21+
if user_regex.search(content[line]):
22+
print(content[line])
23+
file.close()

‎C9_backing_up_a_folder.py

Whitespace-only changes.

‎C9_remove_zeros.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Remove the zeros from files such as spam0042.txt
3+
4+
"""
5+
6+
import os
7+
import re
8+
9+
zeros_regex = re.compile(r'0')
10+
os.chdir(".\\files\\c9_remove_zeros\\")
11+
12+
for folderName, subfolders, filenames in os.walk('.'):
13+
14+
for filename in filenames:
15+
if zeros_regex.search(filename):
16+
filename_new = zeros_regex.sub('', filename)
17+
abs_path = os.path.abspath(folderName)
18+
os.rename(os.path.join(abs_path, filename), os.path.join(abs_path, filename_new))
19+

‎C9_renameDates.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Say your boss emails you thousands of files with American-style dates
3+
(MM-DD-YYYY) in their names and needs them renamed to Europeanstyle
4+
dates (DD-MM-YYYY). This boring task could take all day to do by
5+
hand! Let’s write a program to do it instead.
6+
Here’s what the program does:
7+
• It searches all the filenames in the current working directory for
8+
American-style dates.
9+
• When one is found, it renames the file with the month and day swapped
10+
to make it European-style.
11+
12+
"""
13+
14+
import os
15+
import re
16+
import shutil
17+
18+
# Create a regex that matches files with the American date format.
19+
date_pattern = re.compile(r"""^(.*?)
20+
(([01])?\d)- # one or two digits for the month
21+
(([0123])?\d)- # one or two digits for the day
22+
((19|20)\d\d) # four digits for the year
23+
(.*?)$ # all text after the date
24+
""", re.VERBOSE)
25+
26+
# Loop over the files in the working directory.
27+
os.chdir('.\\files\\c9_dates')
28+
for amer_filename in os.listdir('.'):
29+
mo = date_pattern.search(amer_filename)
30+
31+
# Skip files without a date.
32+
if mo is None:
33+
continue
34+
35+
# Get the different parts of the filename.
36+
beforePart = mo.group(1)
37+
monthPart = mo.group(2)
38+
dayPart = mo.group(4)
39+
yearPart = mo.group(6)
40+
afterPart = mo.group(8)
41+
42+
# Form the European-style filename.
43+
euro_filename = beforePart + dayPart + '-' + monthPart + '-' + yearPart + afterPart
44+
45+
# Get the full, absolute file paths.
46+
abs_working_dir = os.path.abspath('.')
47+
amer_filename = os.path.join(abs_working_dir, amer_filename)
48+
euro_filename = os.path.join(abs_working_dir, euro_filename)
49+
50+
# Rename the files.
51+
shutil.move(amer_filename, euro_filename)

‎files/c8_dir/test1.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Write
2+
a program that opens all .txt files in a folder and searches for any
3+
line
4+
test!
5+
that matches a user-supplied regular expression. The results should
6+
be printed to the screen.
7+
here

‎files/c8_dir/test2.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
here
2+
is
3+
test!
4+
the
5+
second test

‎files/c8_text.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
2+
unaffected by these events.

‎files/c8_text_result.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events.

‎files/c9_dates/03-27-2018.txt

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /