Computer Science

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

COMPUTER SCIENCE

ASSIGNMENT
# Write a program that reads a csv file and creates
another csv file with the same content except the lines
with “hello”.
import csv
row_list=[["S.NO", "Name","Contribution"],
[1., "Hello", "Linux Kernel"],
[2., "Tim Berners-Lee", "World Wide Web"]]
with open("C:\\Users\\kashish\\Desktop\\Program3.csv","w",newline="") as file:
write=csv.writer(file)
write.writerows(row_list)
import csv
reader=csv.reader(open("C:\\Users\\Kashish\\Desktop\\Program3.csv",'r'))
writer=csv.writer(open("C:\\Users\\kashish\\Desktop\\Program4.csv",'w'))
for i in reader:
for j in i:
if j=="Hello":
continue
else:
writer.writerow(i)
# Write a program that write a nested python list to a
csv file. After writing csv file read the csv file and
display the content in tabular format.
import csv
filename="C:\\Users\\chandni\\Desktop\\Program1.csv"
fields=[]
rows=[]
with open(filename,'r') as csvfile:
csvreader=csv.reader(csvfile)
fields=next(csvreader)
for row in csvreader:
rows.append(row)
print("Total no of rows: %d"%(csvreader.line_num))
print('\n')
print('\t '.join(field for field in fields))
for row in rows[:5]:
for col in row:
print(col,end=" ")
print('\n')
#Create a pickled file called ‘colony.dat’ containing the details of a
colony. The strucutre of a colony is as:( colony_code string,
colony_name string, no_of_people int)Write a function in python to
update the file with a new value of no_of_people as per their
colony_code. The value of colony_code and no_of_people are read
during the execution of the program
import pickle

def insertRec():

file=open('C:\\Users\\chandni\\Desktop\\Colony.dat','wb')

for i in range(3):

Colony_Code=input("Enter Colony Code")

Colony_Name=input("Enter Colony Name")

No_Of_People=int(input("Enter No_of People"))

dic={'Colony_Code':Colony_Code,'Colony_Name':Colony_Name,'No of People':No_Of_People}

pickle.dump(dic,file)

file.close()

def updateRecord():

import pickle

Colony_Code=input("Enter Colony Code")

No_Of_People=int(input("Enter No_of People"))

file=open('C:\\Users\\chandni\\Desktop\\Colony.dat','rb')

data=[]

while True:

try:

text=pickle.load(file)

data.append(text)

except EOFError:

pass

file.close()

for i in range(len(data)):

if data[i]['Colony_Code']==Colony_Code:
data[i]['No of People']=No_Of_People

file=open('C:\\Users\\chandni\\Desktop\\Colony.dat','wb')

for i in data:

pickle.dump(i,file)

file.close()

def readRecord():

import pickle

file=open('C:\\Users\\chandni\\Desktop\\Colony.dat','rb')

while True:

try:

text=pickle.load(file)

print(text['Colony_Code'])

print(text['Colony_Name'])

print(text['No Of People'])

except EOFError:

pass

file.close()
# Consider a binary file’student.dat’ which contains the records of
students such as name, rollno, class and marks. Write a program in
python to print the records of only those students from the file who
score marks greater than 60%. import pickle

Import pickle
def count rec():
f=open("student.dat","rb")
num=0
try:
while true:
rec=pickle.load(f)
if rec[3]>60:
print(rec[0],rec[1],rec[2],rec[3])
num+=1
except:
f.close()
return num
# Create a text file VINTAGE.txt containing following information
of carsVNo, VDesc, Vprice . Write a python function to display
details of all those Vintage vehicles which are priced between 200000
and 250000.
file1 = open("myfile.txt","w")
L = ["VNO \n","VDesc\n","VPrice\n"]

# \n is placed to indicate EOL (End of Line)


file1.writelines(L)

file1 = open("myfile.txt","r+")
for VPrice in range(200000-250000)
line=next(file1)
print file1.read()
file1.close()
# Create a file “sports.dat” contains information in following
format: Event- Participant.Write a function in python 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’.

You might also like