file_content = ''
with open("news.txt", mode='r+', encoding='utf-8') as f:
file_content = f.read()
# replace end line and remove trailing whitespace
file_content = file_content.strip().replace('\n', " ")
# remove '.'
file_content = file_content.replace('.', "")
counter = {}
for w in file_content.split(' '):
if counter.get(w):
counter[w] += 1
else:
counter[w] = 1
print(counter)
Q-2
vowels = ['a', 'e', 'o', 'i', 'u', 'y']
file_name = input('Enter file name : ') + ".txt"
file_content = ''
counter = {'vowels': 0, 'consonants': 0}
with open(file_name, mode='+r', encoding='utf-8') as f:
file_content = f.read()
for letter in file_content:
if letter in vowels:
counter['vowels'] += 1
else:
counter['consonants'] += 1
print(counter)
Q-3
file_name = input('Enter file name : ') + ".txt"
file_content = ''
# do some remove end lines , convert to lower case , remove trailing whitespace
with open(file_name, mode='r+', encoding='utf-8') as f:
file_content = f.read().replace('\n', ' ').lower().split(' ')
counter = {}
for w in file_content:
if counter.get(w):
counter[w] += 1
else:
counter[w] = 1
non_duplicated_words = [w for w, c in counter.items() if c == 1]
print(non_duplicated_words)
Q-4
file_name = input('Enter file name : ') + ".txt"
new_file_name = input('Enter the result file name : ') + ".txt"
with open(file_name, mode='r+', encoding='utf-8') as f1, open(new_file_name, mode='w', encoding='utf-8') as f2:
f2.write(f1.read().replace('\n', ' '))
Q-5
lines = int(input('number of first lines to be read :'))
with open('news.txt', 'r+', encoding='utf-8') as f:
for l in range(lines):
print(f.readline().removesuffix('\n'))
Q-6
lines = int(input('number of last lines to be read :'))
result = []
with open('news.txt', 'r+', encoding='utf-8') as f:
result = f.readlines()
for l in result[len(result) - lines:]:
print(l.replace('\n', '').strip())
Q-7
file_name = "news.txt"
new_file_name = "new news.txt"
combined = []
with open(file_name, mode='r+', encoding='utf-8') as f1, open(new_file_name, mode='r+', encoding='utf-8') as f2:
combined = list(zip(f1.readlines(), f2.readlines()))
print(combined)
Q-8
file_name = input('Enter file name : ') + ".txt"
file_content = ''
# do some remove end lines , convert to lower case , remove trailing whitespace
with open(file_name, mode='r+', encoding='utf-8') as f:
file_content = f.read().replace('\n', '').replace(' ', '').lower()
counter = {}
for w in file_content:
if counter.get(w):
counter[w] += 1
else:
counter[w] = 1
for k, v in counter.items():
print(f'{k} : {v}')
Q-9
file_name = input('Enter file name : ') + ".csv"
new_file_name = input('Enter the result file name : ') + ".csv"
with open(file_name, mode='r+', encoding='utf-8') as f1, open(new_file_name, mode='w', encoding='utf-8') as f2:
f2.write(f1.read())
import csv
file_name = 'employees.csv'
with open(file_name, mode='r+', newline="", encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile)
for row in csv_reader:
if 'cs' in row:
print(row[0])
Q-1
Q-2
Q-3
Q-4
Q-5
Q-6
Q-7
Q-8
Q-9
Q-10
Q-11