0% found this document useful (0 votes)
3 views4 pages

Python cycle 5

The document outlines a Python programming lab cycle with four exercises. The exercises include reading a file into a list, copying odd lines from one file to another, reading rows from a CSV file, and printing specific columns from a CSV file. Each exercise includes code snippets and output examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Python cycle 5

The document outlines a Python programming lab cycle with four exercises. The exercises include reading a file into a list, copying odd lines from one file to another, reading rows from a CSV file, and printing specific columns from a CSV file. Each exercise includes code snippets and output examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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("demo1.txt",'w')
f1.write
("This the new file i have created\n here is the second line\n each line
in a list")
f1.close()
f=open("demo1.txt",'r')
lines=[]
for line in f:
lines.append(line.strip())
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 = src.readlines()[::2]
with open('destination_file.txt', 'w') as dest:
dest.writelines(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('color.csv','r') as file:
csv_reader = csv.reader(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('color.csv', newline='') as csvfile:
data = csv.DictReader(csvfile)
print("Hexadecimal code & RGB Representation")
print("---------------------------------")
for row in data:
print(row['HEX'], row['RGB'])

OUTPUT

You might also like