Computer Science
A file sports.dat contains information in following format:
Event - Participant
Write a function that would read contents from file sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is "Atheletics".
Python File Handling
2 Likes
Answer
Let the file "sports.dat" include the following sample records:
Athletics - Rahul
Swimming - Tanvi
Athletics - Akash
Cycling - Kabir
Athletics - Riya
def filter_records(input_file, output_file):
with open(input_file, 'r') as f_in:
with open(output_file, 'w') as f_out:
for line in f_in:
event, participant = line.strip().split(' - ')
if event == 'Athletics':
f_out.write(line)
filter_records('sports.dat', 'Athletic.dat')
The file "Atheletic.dat" includes following records:
Athletics - Rahul
Athletics - Akash
Athletics - Riya
Answered By
2 Likes
Related Questions
Identify the error in the following code.
import pickle data = ['one', 2, [3, 4, 5]] with open('data.dat', 'wb' : pickle.dump(data)
Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.
A file contains a list of telephone numbers in the following form:
Arvind 7258031 Sachin 7259197
The names contain only one word, the names and telephone numbers are separated by white spaces. Write program to read a file and display its contents in two columns.
Write a program to count the words "to" and "the" present in a text file "Poem.txt".