Python Programming Lab Cycle 5
1. Write a Python program to read a file line by line and store it into a list.
f1=open("[Link]",'w')
[Link]
("This the new file i have created\n here is the second line\n each line
in a list")
[Link]()
f=open("[Link]",'r')
lines=[]
for line in f:
[Link]([Link]())
print(lines)
output
p
2. Python program to copy odd lines of one file to other
print("Name: Annu Bindhu Benoy")
print("Roll No: 019")
print("Experiment No: 16")
source_file = "source_file.txt"
destination_file = "destination_file.txt"
with open('source_file.txt', 'r') as src:
lines = [Link]()[::2]
with open('destination_file.txt', 'w') as dest:
[Link](lines)
print(lines)
Output
3. Write a Python program to read each row from a given csv file and print a list of
strings.
print("Name:Annu Bindhu Benoy")
print("Admission Number:A24MCA014")
print("Experiment No: 17")
import csv
with open('[Link]','r') as file:
csv_reader = [Link](file)
for row in csv_reader:
print(row)
output
4. Write a Python program to read specific columns of a given CSV file and print the
content of the columns.
print("Name: ANNU Bindhu Benoy")
print("Admisssion No:A24MCA014")
print("Experiment No: 18")
import csv
With open('[Link]', newline='') as csvfile:
data = [Link](csvfile)
print("Hexadecimal code & RGB Representation")
print("---------------------------------")
for row in data:
print(row['HEX'], row['RGB'])
OUTPUT