हिंदी
तमिलनाडु बोर्ड ऑफ सेकेंडरी एज्युकेशनएचएससी विज्ञान कक्षा १२

Write the different methods to read a File in Python. - Computer Science

Advertisements
Advertisements

प्रश्न

Write the different methods to read a File in Python.

संक्षेप में उत्तर

उत्तर

There are two ways to read a CSV file.

  1. Use the CSV module’s reader function
  2. Use the DictReader class.

CSV module’s reader function:

  • We can read the contents of the CSV file with the help of CSV. reader() method.
  • The reader function is designed to take each line of the file and make a list of all columns.
  • Using this method one can read data from CSV files of different formats like quotes (” “), pipe (|), and comma (,).

Syntax for CSV.reader(): .
CSV.reader( fileobject,delimiter,fmtparams)
where

  • file object: passes the path and the mode of the file
  • delimiter: an optional parameter containing the standard dilects like |, etc can be omitted.
  • Fmtparams: optional parameter which helps to override the default values of the dialects like skipinitialspace, quoting, etc. can be omitted.

Program:

#importing csv
import csv
#opening the csv file which is in different
location with read mode
with opent(‘c.\ \pvprg\ \samplel-csv’, ‘r’) as F:
#other way to open the file is f= (‘c:\ \
pyprg\ \ samplel.csv’, ‘r’)
reader = csv.reader(F)
#printing each line of the Data row by row
print(row)
F.close()

Output:

[‘SNO’, ‘NAME’, ‘CITY’]
[‘12101’, ‘RAM’, ‘CHENNAI’]
[‘12102’, ‘LAVANYA’, ‘TIRCHY’]
[‘12103’, ‘LAKSHMAN’, ‘MADURAI’]

Reading CSV File into A Dictionary:

  • To read a CSV file into a dictionary can be done by using DictReader class of CSV module which works similar to the reader() class but creates an object which maps data to a dictionary.
  • The keys are given by the field names as parameters.
  • DictReader works by reading the first line of the CSV and using each comma-separated value in this line as a dictionary key.
  • The columns in each subsequent row then behave like dictionary values and can be accessed with the appropriate key (i.e. fieldname).
import csv
filename = ‘c:\\pyprg\ \sample8.csv’
inputfile =csv.DictReader( opet(filename’r’))
for row in inputfile:
print(dict(row)) #dict() to print data

Output:

{‘ItemName’: ‘Keyboard ” ‘Quantity’: ’48’}
{‘ItemName ‘: ‘Monitor: ‘Quantity’: ’52’}
{‘ItemName ‘: ‘Mouse ” ‘Quantity’: ’20’}

shaalaa.com
Read and Write a CSV File Using Python
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 13: Python and CSV files - Evaluation [पृष्ठ २६६]

APPEARS IN

सामाचीर कलवी Computer Science [English] Class 12 TN Board
अध्याय 13 Python and CSV files
Evaluation | Q 3. | पृष्ठ २६६

संबंधित प्रश्न

Mention the two ways to read a CSV file using Python.


Mention the default modes of the File.


Write a Python program to read a CSV file with default delimiter comma (,).


What is the difference between the write mode and append mode?


What is the difference between reader() and DictReader() function?


Write a Python program to write a CSV File with custom quotes.


Write the rules to be followed to format the data in a CSV file.


What is the advantage of using a CSV file for permanent storage? Write a Program in Python that defines and calls the following user-defined functions:

  1. ADD() - To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of a list with field elements such as empid, name, and mobile to store an employee id, employee name, and employee salary respectively.
  2. COUNTR() - To count the number of records present in the CSV file named ‘record.csv’.

Give any one point of difference between a binary file and a CSV file. Write a Program in Python that defines and calls the following user-defined functions:

  1. add() - To accept and add data of an employee to a CSV file ‘furdata.csv’. Each record consists of a list with field elements such as fid, fname, and fprice to store furniture id, furniture name, and furniture price respectively.
  2. search() - To display the records of the furniture whose price is more than 10000.

Select the correct output of the code:

S="Amrit Mahotsav @ 75"
A=S.partition (" ")
print(a)

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×