Search This Blog

Saturday, 18 December 2021

Program to read each row from a given CSV file

Example 1 : Write a Python program to read each row from a given csv file and print a list of strings.
Note : delimiter: A one-character string used to separate fields. It defaults to ','.

quotechar: The quotechar optional parameter tells the writer which character to use to quote fields when writing.

Python code
	
import csv
with open('departments.csv', newline='') as csvfile:
 data = csv.reader(csvfile, delimiter=' ', quotechar='|')
 for row in data:
   print(', '.join(row))	
	
departments.csv
	
department_id,department_name,manager_id,location_id
10,Administration,200,1700
20,Marketing,201,1800
30,Purchasing,114,1700
40,Human Resources,203,2400
50,Shipping,121,1500
60,IT,103,1400
70,Public Relations,204,2700
80,Sales,145,2500
90,Executive,100,1700
100,Finance,108,1700
110,Accounting,205,1700
120,Treasury,,1700
130,Corporate Tax,,1700
140,Control And Credit,,1700
150,Shareholder Services,,1700
160,Benefits,,1700
170,Manufacturing,,1700
180,Construction,,1700
190,Contracting,,1700
200,Operations,,1700
210,IT Support,,1700
220,NOC,,1700
230,IT Helpdesk,,1700
240,Government Sales,,1700
250,Retail Sales,,1700
260,Recruiting,,1700
270,Payroll,,1700
	
Output:
	
department_id,department_name,manager_id,location_id
10,Administration,200,1700
20,Marketing,201,1800
30,Purchasing,114,1700
40,Human, Resources,203,2400
50,Shipping,121,1500
60,IT,103,1400
70,Public, Relations,204,2700
80,Sales,145,2500
90,Executive,100,1700
100,Finance,108,1700
110,Accounting,205,1700
120,Treasury,,1700
130,Corporate, Tax,,1700
140,Control, And, Credit,,1700
150,Shareholder, Services,,1700
160,Benefits,,1700
170,Manufacturing,,1700
180,Construction,,1700
190,Contracting,,1700
200,Operations,,1700
210,IT, Support,,1700
220,NOC,,1700
230,IT, Helpdesk,,1700
240,Government, Sales,,1700
250,Retail, Sales,,1700
260,Recruiting,,1700
270,Payroll,,1700	
	

No comments:

Post a Comment

how to implement YOLOv3 using Python and TensorFlow

Object Detection with YOLOv3 Introduction YOLOv3 (You Only Look Once version 3) is a real-time object detection algorithm that can detect ob...