Saturday, July 11, 2020

Pyhton Accessing CSV File ,Reading and Writiing into CSV File Class 12 Sumita Arora Practice




CSV File

CSV (Comma Separated Values) is a simple file format used to store data in tabular form such as excel sheet or database. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The csv module  is used  for working with CSV files in python. It is used to read and write tabular data in CSV format.

 open notpad and type contents as
name,class, marks
ajay,12,75
vijay,11,85
then save the file named as"abc.csv"

# reading csv file contents

import csv 
f=open("abc.csv",'r')
r=csv.reader(f)  # canvert file object into reader object
for row in r:
        print(row)




Every record is stored in reader object in form of List 

2.





3.

join() is string method that join all  values of each row with comma separator

4.



5.

6.



7.


8

Contents of student.csv file
name,class, marks
Vinod,12,75
Vijay,11,85
Sunil,12,65
Mukesh,10,75
Rajat,10,85
Kamal,12,70
Amit,11,95


Python Binary File Program Solution from Sumita Arora Text Book Question 's


1.Write dictionary into a binary file

import pickle
d={'python':90,'java':80,'C++':85}
f=open('abc.dat','wb')   # open binary file in write mode 
pickle.dump(d,f# dump d into f
f.close()

2. Read dictionary from  a binary file
import pickle  

f=open('abc.dat','rb')   # rb read binary file mode as rb
dic=pickle.load(f)  #Read object from a pickle file or binary file
f.close()
print(dic)

3. Writing and reading integers from binary file
import pickle  
f=open('numbers.dat','wb')
n=int(input("how many number you want to enter?"))
for i in range(n):
    x=int(input("enter number\n"))
    pickle.dump(x,f)
f.close()
f=open('numbers.dat','rb')
for i in range(n):
    y=pickle.load(f)  #Read object from a pickle file or binary file
    print(y)
f.close()

4. Writing List in binary file
import pickle
list=[1,2,3,4,5,6,7,8,9,10]
f=open('abc.dat','wb')
pickle.dump(list,f# dump list into f
f.close()


5. Write employee name and salary into binary file

import pickle
f=open('emp.dat','wb')
n=int(input("Enter no of employees\n"))
d={}
i=1
while i<=n:
              name=input("Enter name")
              salary=int(input("Enter salary"))
              d[name]=salary
              i=i+1

pickle.dump(d,f)  # dump d into f
f.close()

6. Reading employ name and salary where salary greater then 4000 from binary file emp.dat
import pickle
f=open("emp.dat","rb")
rd=pickle.load(f)
for key,value in rd.items():
              if(value>4000):

                            print(key,":",value)



Python Text File Writing Based Program Based on Sumita Arora Text Book


1. Reading lines from one file and write into another file starting from 's'

r=open('read.txt',"r")  # open file in read mode
w=open("write.txt",'w') # open file in write mode
lines=r.readlines()
for line in lines:
    if(line[0]=='s'):    # write all lines start from 's' from read.txt to write.txt
        w.write(line)
r.close()
w.close()

2. Write first and last line of one file into another file

r=open('read.txt',"r")  # open file in read mode
w=open("write.txt",'w') # open file in write mode
lines=r.readlines()
w.write(lines[0])   # write  first line 
w.write(lines[-1])    # write  last line 
r.close()
w.close()

3. Write lines of one file into anther in reverse form

r=open('read.txt',"r")
w=open("write.txt",'w')
lines=r.readlines()
for line in lines:
    w.write(line[::-1])  # write line in reverse form

r.close()

w.close()

contents of read.txt
my name is mukesh

pgt computer Science

Contents of write.txt file
hsekum si eman ym

ecneicS retupmoc tgp
4. Write only digits from one file to another
fr=open('read.txt',"r")
fw=open("write.txt",'w')
s=fr.read()
w=s.split()
for i in w:
    for c in i:
                  if c.isdigit():  # check  character for digit
                                fw.write(c)
fr.close()
fw.close()

Contents in read.txt
pin code 160014
Mobile no 9646648307
E=mail id: mk168in@gmail.com

Contents in write.txt file
1600149646648307168


5.Writing upper,lower case characters and other  characters input by keyboard in different  files
f1=open('lower.txt',"w")
f2=open('upper.txt',"w")
f3=open('other.txt',"w")
n=int(input("How many characters you want to enter "))
for i in range(n):
      ch=input("Enter a character")
      if ch.islower():
          f1.write(ch)
      elif ch.isupper():
          f2.write(ch)
      else:
          f3.write(ch)
f1.close()
f2.close()
f3.close()


Python Text File Program Solution from Sumita Arora Book


Count characters, words and lines present in a text file using python program


Note : text file and python program must present in same foloder

Count words in a file
f=open('abc.txt')
c=0
s=f.read()  # read file and store the content of file in a string s
w=s.split() # split the contents of s and store it into list w 
for i in w:
    c=c+1
print(c)
f.close()

Count ‘india’ word from file

f=open('abc.txt')
c=0
s=f.read()
w=s.split()
for i in w:
    if(i=="india"):
        c=c+1
print(c)
f.close()

Count  and print words having length <=3  from file

f=open('abc.txt')
c=0
s=f.read()
w=s.split()
for i in w:
    if(len(i)<=3):  #match the length of item in list
        print(i)
        c=c+1
print(c)


Count lines in a file
f=open('abc.txt')
c=0
list=f.readlines() # Read lines from file and store in a list named list
for i in list:
    c=c+1
print(c)
f.close()

Count lines starts from ‘s’ 

f=open('abc.txt')
c=0
list=f.readlines()
for i in list:
    if(i[0]=='s'): # match each item of list starts with s or not
        c=c+1
print(c)
f.close()

Count characters from file
f=open('abc.txt')
count=0
s=f.read()
w=s.split()
for i in w:      # traverse every item in list
    for c in i:  # traverse every character in item
        count=count+1
print(count)



Count lower case alphabets from file

f=open('abc.txt')
count=0
s=f.read()
w=s.split()
for i in w:
    for c in i:
        if(i.islower()): # find the given character is lower case or not
            count=count+1

print(count)


Count number of  digits present in text file

f=open('abc.txt')
count=0
s=f.read()
w=s.split()
for i in w:
    for c in i:
        if(i.isdigit()): # find the given character is digit or not
            count=count+1


print(count)

Print first and last line of text file
f=open('abc.txt')
lines=f.readlines()
print(lines[-1])   # -1 is the index of last item in a list
print(lines[0])    # 0 in the index of first item in a list


Print longest line from  a text file

f=open('abc.txt')
c=0
list=f.readlines()
longest=""
for i in list:
    if(len(i)>len(longest)): 
        longest=i
print(longest)

Program to display size of a file in bytes

f=open('abc.txt')
s=f.read()  # read file and store the content of file in a string s
size=len(s)
print("size in bytes:",size)