📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 230question

PYTHON AND CSV FILES · Part 9

Chapter 2: 3 · COMPUTER SCIENCE

Then, we used csv. to display all the characters after double quotes. 12th Computer Chapter - - . .

. . CSV files with Custom Delimiters You can read CSV file having custom delimiter by registering a new dialect with the help of csv. ().

In the following file called “sample4.csv”,each column is separated with | (Pipe symbol) Roll No | Name | City 12101 | Arun | Chennai 12102 | Meena | Kovai 12103 | Ram | Nellai The following program read the file “sample4.csv” with user defined delimiter “|” import csv csv. ('myDialect', delimiter = '|') with open('c:\pyprg\sample4.csv', 'r', newline‘=’) as f: reader = csv.reader(f, dialect='myDialect') for row in reader: print(row) f.close() OUTPUT ['RollNo', 'Name', 'City'] ['12101', 'Arun', 'Chennai'] ['12102', 'Meena', 'Kovai'] ['12103', 'Ram', 'Nellai'] In the above program, a new dialects called myDialect is registered. Use the delimiter=| where a pipe (|) is considered as column separator. .

. Read a specific column In a File To get the specific columns like only Item Name and profit for the “sample5.csv” file . Then you have to do the following: import csv #opening the csv file which is in different location with read mode f=open("c:\pyprg\ch13sample5.csv",'r') #reading the File with the help of csv.reader() readFile=csv.reader(f) #printing the selected column for col in readFile : print (col[ ],col[ ]) f.close() sample5.csv File in Excel 12th Computer Chapter - - Python and CSV Files A B C D item Nam Cost-Rs Quantity Profit Keyboard Monitor 10400 Mouse sample5.csv File with selected col OUTPUT Item Name Profit Keyboard Monitor 10400 Mouse . .

Read A CSV File And Store It In A List In this topic you are going to read a CSV file and the contents of the file will be stored as a list. The syntax for storing in the List is list = [] # Start as the empty list list.append(element) # Use append() to add elements For example all the row values of “sample.csv” file is stored in a list using the following program

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →