Alexander Hess
a37c87d9c8
- add the code files provided by the instructor - the programming/files folder with the data files is NOT included here due to its size - add a .gitignore file to exclude the data files' folder
88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Fri Feb 15 21:37:53 2019
|
|
|
|
@author: Alexander Hillert, Goethe University Frankfurt
|
|
"""
|
|
|
|
# Please adjust the directory to your machine.
|
|
directory="C:/Lehre/Textual Analysis/Programming/Files/"
|
|
# It is important to use a single forward slash / but not a single backslash \.
|
|
|
|
# For MAC users: your directory will usually start with "/Users/". For example:
|
|
#directory="/Users/FirstnameLastname/Textual Analysis/Programming/Files/"
|
|
|
|
# open the Fun_with_Python text file
|
|
input_file=open(directory+"Fun_with_Python.txt","r")
|
|
|
|
###################################
|
|
# Programming Problem 1
|
|
###################################
|
|
|
|
# Task 1: open the file 'Fun_with_Python.txt' in Spyder and print its content
|
|
# The file can be found in our data folder
|
|
|
|
# get the text from the file
|
|
input_text= TO BE COMPLETED
|
|
# print the content, i.e., the text of the file (previous line)
|
|
print(TO BE COMPLETED)
|
|
|
|
# See slide 7
|
|
|
|
|
|
# Task 2: Write the content of 'Fun_with_Python.txt' to a new text file
|
|
# with the name 'More_fun_with_Python.txt'.
|
|
|
|
# ENTER YOUR COMMANDS HERE
|
|
# See slide 8.
|
|
# REMEMBER to close your file. If you do not close the new txt file, its content
|
|
# will not be saved to the hard drive. You will find an empty txt in your file manager.
|
|
|
|
|
|
# Task 3: Write a loop that prints some text (whatever you like) ten times.
|
|
|
|
# ENTER YOUR COMMANDS HERE
|
|
# See slide 9.
|
|
# You have several options. While loop, for X in range() loop, etc.
|
|
|
|
|
|
|
|
# Task 4: Print the text of the "Fun_with_Python" file line by line!
|
|
|
|
# ENTER YOUR COMMANDS HERE
|
|
# See slide 10.
|
|
# You need a loop (Task 3) and in each iteration of the loop have Python print
|
|
# a line of text.
|
|
|
|
|
|
|
|
# Task 5: Count how often the word 'good' appears in the document 'Fun_with_Python.txt'!
|
|
|
|
# ENTER YOUR COMMANDS HERE
|
|
# See slide 11.
|
|
|
|
|
|
|
|
# Task 6a: Now, print only the lines that contain the word 'good'!
|
|
|
|
# ENTER YOUR COMMANDS HERE
|
|
# See also slide 12.
|
|
# You can use the line-by-line printing from Task 4 and combine it with the command ".count()" from Task 5
|
|
# and add the if condition from slide 12.
|
|
# If condition: for each line check whether the specific line contains the word "good".
|
|
|
|
|
|
|
|
# Task 7: print only the lines that start with the word 'This'!
|
|
|
|
# ENTER YOUR COMMANDS HERE
|
|
# See slide 15.
|
|
# This is very similar to task 6. You only need to modify the if condition a bit.
|
|
|
|
|
|
|
|
|
|
# Task 8a: Replace the word "good" by "excellent" and display the new text!
|
|
# See slide 16.
|
|
# ENTER YOUR COMMANDS HERE
|
|
|