Lab Report 05
Lab Report 05
Lab Report 05
#code:
def uppercase(file_name):
try:
with open(file_name) as file:
for line in file:
print(line.upper(), end='')
except:
print(f"File '{file_name}' not found.")
file_name = input("Enter a file name: ")
uppercase(file_name)
#output:
Enter a file name: ashraful.txt
File 'ashraful.txt' not found.
#code:
name = input("file name: ")
try:
file = open(name, 'r')
except:
print("not found.")
exit()
count = 0
t_c = 0.0
for line in file:
if line.startswith("X-DSPAM-Confidence:"):
count += 1
Lab Report 05 1
colon_index = line.find(":")
confidence = float(line[colon_index + 1:].strip())
t_c += confidence
file.close()
if count > 0:
a_c = t_c / count
print("average spam confidence:", a_c)
else:
print("no lines with spam confidence found.")
#output:
file name: mbox-short.txt
average spam confidence: 0.7507185185185187
#code:
def extract_unique_words(file_name):
unique_words = []
try:
with open(file_name, 'r') as file:
for line in file:
words = line.split()
for word in words:
if word not in unique_words:
Lab Report 05 2
unique_words.append(word)
except:
print(f"File '{file_name}' not found.")
return []
return sorted(unique_words)
# Provide the file name or its full path
file_name = 'romeo.txt'
# Call the function to extract unique words and print them
unique_words = extract_unique_words(file_name)
for word in unique_words:
print(word)
#output:
'Arise', 'But', 'It', 'Juliet', 'Who', 'already',
'and', 'breaks', 'east', 'envious', 'fair', 'grief',
'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft',
'sun', 'the', 'through', 'what', 'window',
'with', 'yonder'
import string
def extract_unique_words(file_name):
unique_words = []
stop_words = ["but", "is", "the", "with", "and"]
punctuation = set(string.punctuation)
try:
with open(file_name, 'r') as file:
for line in file:
line = line.strip().lower()
words = ''
for ch in line:
if ch not in punctuation:
words += ch
words = words.split()
words = [word for word in words if word.lower() not in stop_words and word not in unique_w
ords]
unique_words.extend(words)
except FileNotFoundError:
print(f"File '{file_name}' not found.")
return []
return sorted(unique_words)
# Provide the file name or its full path
Lab Report 05 3
file_name = 'romeo.txt'
# Call the function to extract unique words after removing stopwords
unique_words = extract_unique_words(file_name)
# Print the unique words
for word in unique_words:
print(word)
#output:
'Arise', 'It', 'Juliet', 'Who', 'already','breaks', 'east', 'envious', 'fair', 'grief', 'k
ill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'through', 'what', 'window', 'yond
er'
#code:
# Prompt for the file name
file_name = input("Enter a file name: ")
# Initialize variables
sender_emails = []
from_line_count = 0
try:
with open(file_name, 'r') as file:
for line in file:
if line.startswith('From '):
# Split the line into words
words = line.split()
# Extract the sender's email address
sender_email = words[1]
# Print the sender's email address
print(sender_email)
# Add the email address to the list
sender_emails.append(sender_email)
# Increment the from_line_count
from_line_count += 1
except FileNotFoundError:
print(f"File '{file_name}' not found.")
# Print the total count of From lines
print(f"There were {from_line_count} lines in the file with From as the first word")
Lab Report 05 4
[email protected]
.....
#code
numbers = [] # List to store the numbers
while True:
user_input = input("Enter a number (or 'done' to finish): ")
if user_input == 'done':
break
try:
number = float(user_input)
numbers.append(number)
except ValueError:
print("Invalid input. Please enter a number or 'done'.")
if numbers:
maximum = max(numbers)
minimum = min(numbers)
print("Maximum number:", maximum)
print("Minimum number:", minimum)
else:
print("No numbers entered.")
#output:
Enter a number (or 'done' to finish): 59
Enter a number (or 'done' to finish): 10
Enter a number (or 'done' to finish): 48
Enter a number (or 'done' to finish): done
Maximum number: 59.0
Minimum number: 10.0
Lab Report 05 5