Computer Science
Write a program to search the names and addresses of persons having age more than 30 in the data list of persons stored in a text file.
Python Data Handling
2 Likes
Answer
Let the file "Persons.txt" include the following sample text:
Samyukta, Mumbai, 35
Anubhav, Chennai, 28
Aniket, Hyderabad, 42
Sarth, Bangalore, 31
f = open('Persons.txt', 'r')
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
if len(data) >= 3 and int(data[2]) > 30:
print('Name:', data[0], 'Address:', data[1])
f.close()
Output
Name: Samyukta Address: Mumbai
Name: Aniket Address: Hyderabad
Name: Sarth Address: Bangalore
Answered By
2 Likes
Related Questions
Write a program that appends the contents of one file to another. Have the program take the filenames from the user.
Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside file OTHERS.
Write a function in Python to count and display the number of lines starting with alphabet 'A' present in a text file "LINES.TXT", e.g., the file "LINES.TXT" contains the following lines:
A boy is playing there. There is a playground. An aeroplane is in the sky. A cricket match is being played.
The function should display the output as 3.
Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files:
- a text file “example.txt” in both read and write mode.
- a binary file “bfile.dat” in write mode.
- a text file “try.txt” in append and read mode.
- a binary file “btry.dat” in read only mode.