0% found this document useful (0 votes)
272 views13 pages

Reading and Writing CSV Files

The document discusses how to read and write different types of CSV files in Python using the csv module. It provides examples of reading CSV files with different delimiters like comma, pipe, and quotes. It also demonstrates how to read CSV files into dictionaries, modify existing CSV files, and write data to CSV files. The key points covered are: 1) The csv.reader() and csv.writer() functions are used to read and write CSV files respectively. 2) The csv.register_dialect() method allows specifying file formatting parameters like delimiter and quoting. 3) The csv.DictReader() class reads CSV rows into OrderedDict objects for easy access by column name. 4) Existing CSV files can

Uploaded by

Surya Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
272 views13 pages

Reading and Writing CSV Files

The document discusses how to read and write different types of CSV files in Python using the csv module. It provides examples of reading CSV files with different delimiters like comma, pipe, and quotes. It also demonstrates how to read CSV files into dictionaries, modify existing CSV files, and write data to CSV files. The key points covered are: 1) The csv.reader() and csv.writer() functions are used to read and write CSV files respectively. 2) The csv.register_dialect() method allows specifying file formatting parameters like delimiter and quoting. 3) The csv.DictReader() class reads CSV rows into OrderedDict objects for easy access by column name. 4) Existing CSV files can

Uploaded by

Surya Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Reading different types of CSV files

In python, we use csv.reader() module to read the csv file. Here, we will show you how
to read different types of csv files with different delimiter
like quotes(""), pipe(|) and comma(,).
Normal CSV file
We have a csv file called people.csv having default delimiter comma(,) with following
data:

SN, Name, City

1, John, Washington

2, Eric, Los Angeles

3, Brad, Texas

Example 1: Read people.csv file, where delimiter is comma (,)


1. import csv
2.
3. with open('people.csv', 'r') as csvFile:
4. reader = csv.reader(csvFile)
5. for row in reader:
6. print(row)
7.
8. csvFile.close()

When we run the above program, the output will be

['SN', ' Name', ' City']


['1', ' John', ' Washington']
['2', ' Eric', ' Los Angeles']
['3', ' Brad', ' Texas']

In the above program, we read the people.csv file. Then, we print the row of each
columns.

CSV files with Initial Spaces


As you can see in Example 1, we had spaces after the delimeter due to which we got
spaces in the output too. Well, for this kind of files csv() library has a solution for the
programmers.
We can read the csv file and remove whitespaces, by registering new
dialects using csv.register_dialect() class of csv module. A dialect describes the
format of the csv file that is to be read.
In dialects we have a parameter skipinitialspace which is used for removing
whitespaces after the delimeter. By default it has value false but in our situation we
have to make it true.

We will use people.csv which is used in Example 1, with following data

SN, Name, City

1, John, Washington

2, Eric, Los Angeles

3, Brad, Texas

Example 2: Read people.csv file, where after delimiter we have


spaces
1. import csv
2.
3. csv.register_dialect('myDialect',
4. delimiter = ',',
5. skipinitialspace=True)
6.
7. with open('people.csv', 'r') as csvFile:
8. reader = csv.reader(csvFile, dialect='myDialect')
9. for row in reader:
10. print(row)
11.
12. csvFile.close()

When we run the above program, the output will be

['SN', 'Name', 'City']


['1', 'John', 'Washington']
['2', 'Eric', 'Los Angeles']
['3', 'Brad', 'Texas']
In the above program we registered a new dialect with delimiter =
',' and skipinitialspace=True which tells the compiler that there are whitespaces after
the delimiter.
*Note: A dialect is a class of csv module which helps to define parameters for reading
and writing CSV. It allows you to create, store, and re-use various formatting
parameters for your data.

CSV file with quotes


We can read the csv file with quotes, by registering new
dialects using csv.register_dialect() class of csv module.
Here, we have quotes.csv file with following data.

"SN", "Name", "Quotes"

1, Buddha, "What we think we become"

2, Mark Twain, "Never regret anything that made you smile"

3, Oscar Wilde, "Be yourself everyone else is already taken"

Example 3: Read quotes.csv file, where delimiter is comma(,) but


with quotes
1. import csv
2.
3. csv.register_dialect('myDialect',
4. delimiter = ',',
5. quoting=csv.QUOTE_ALL,
6. skipinitialspace=True)
7.
8. with open('quotes.csv', 'r') as f:
9. reader = csv.reader(f, dialect='myDialect')
10. for row in reader:
11. print(row[2])

When we run the above program, the output will be

Quotes
What we think we become
Never regret anything that made you smile
Be yourself everyone else is already taken
In the above program, we register a dialect with name myDialect. Then, we
used csv.QUOTE_ALL to display all the characters after double quotes.

CSV files with Custom Delimiters


We can read csv file having custom delimeter by registering a new dialect with the help
of csv.register_dialect().
Here, we have following data in file called dialects.csv.

"pencil"|"eraser"|"sharpner"

"book"|"chair"|"table"

"apple"|"mango"|"grapes"

Example 4: Read dialects.csv file


1. import csv
2.
3. csv.register_dialect('myDialect', delimiter = '|')
4.
5. with open('dialects.csv', 'r') as f:
6. reader = csv.reader(f, dialect='myDialect')
7. for row in reader:
8. print(row)

When we run the above program, the output will be

['pencil', 'eraser', 'sharpner']

['book', 'chair', 'table']

['apple', 'mango', 'grapes']

In the above program, we register a new dialects as myDialect. Then, we


use delimiter=| where a pipe(|) is considered as column separator.

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 fieldnames parameter.

Example 5: Read people.csv file into a dictionary


1. import csv
2.
3. with open("people.csv", 'r') as csvfile:
4. reader = csv.DictReader(csvfile)
5. for row in reader:
6. print(dict(row))
7.
8. csvFile.close()

When we run the above program, the output will be

{'SN': '1', ' Name': ' John', ' City': ' Washington'}
{'SN': '2', ' Name': ' Eric', ' City': ' Los Angeles'}
{'SN': '3', ' Name': ' Brad', ' City': ' Texas'}

In above program, we use DictReader() to read people.csv file and map into a
dictionary. Then, we use dict() to print the data in dictionary format without order.
If we remove dict() function from the above program and only used print(row), output
will be
OrderedDict([('SN', '1'), (' Name', ' John'), (' City', ' Washington')])
OrderedDict([('SN', '2'), (' Name', ' Eric'), (' City', ' Los Angeles')])
OrderedDict([('SN', '3'), (' Name', ' Brad'), (' City', ' Texas')])

We can also register new dialects and use it in the DictReader() methods. Suppose we
have a people_data.csv in the following format

SN| Name| City

1| John| Washington

2| Eric| Los Angeles

3| Brad| Texas

Example 6: Read people_data.csv into a dictionary by registering a


new dialect
1. import csv
2.
3. csv.register_dialect('myDialect',
4. delimiter = '|',
5. skipinitialspace=True)
6.
7. with open("people.csv", 'r') as csvfile:
8. reader = csv.DictReader(csvfile, dialect='myDialect')
9. for row in reader:
10. print(row)
11.
12. csvfile.close()

When we run the above program, the output will be

OrderedDict([('SN', '1'), ('Name', 'John'), ('City', 'Washington')])


OrderedDict([('SN', '2'), ('Name', 'Eric'), ('City', 'Los Angeles')])
OrderedDict([('SN', '3'), ('Name', 'Brad'), ('City', 'Texas')])

*Note: In case of python 3, using DictReader() gives OrderedDict by default. An


OrderedDict is a dictionary subclass which saves the order in which its contents are
added.
Writing data into different types of CSV files

In Python we use csv.writer() module to write data into csv files. This module is similar
to the csv.reader() module.
Writing on Existing File
We have a people.csv file with following data.

SN, Name, City

1, John, Washington

2, Eric, Los Angeles

3, Brad, Texas

Now, we are going to modify people.csv file.

Example 1: Modifying existing rows of people.csv


1. import csv
2.
3. row = ['2', ' Marie', ' California']
4.
5. with open('people.csv', 'r') as readFile:
6. reader = csv.reader(readFile)
7. lines = list(reader)
8. lines[2] = row
9.
10. with open('people.csv', 'w') as writeFile:
11. writer = csv.writer(writeFile)
12. writer.writerows(lines)
13.
14. readFile.close()
15. writeFile.close()
When we open the people.csv file with text editor, then it will show:
SN, Name, City
1, John, Washington
2, Marie, California
3, Brad, Texas

In the above program, we modified the third row of people.csv and saved the result. At
first, we read the people.csv file using csv.reader() function. Then, we
used list() function to convert all the csv data in a list and store in lines. After that, we
changed third row of csv file with row i.e lines[2] = row. Finally, we write the values
of lines list to people.csv file.
Sometimes, we may need to add new rows in the existing csv file. So, we are going to
append a new row to people.csv file used in Example 1.

Example 2: Appending new rows to people.csv file


1. import csv
2.
3. row = ['4', ' Danny', ' New York']
4.
5. with open('people1.csv', 'a') as csvFile:
6. writer = csv.writer(csvFile)
7. writer.writerow(row)
8.
9. csvFile.close()
When we open people.csv file with text editor, then it will show :
SN, Name, City
1, John, Washington
2, Marie, California
3, Brad, Texas
4, Danny, New York

In the above program, we append a new row into people.csv. For this, we opened the
csv file in 'a' append mode. Then, we write the value of row after the last line of
the people.csv file.

Normal CSV File


We create a normal csv file using writer() method of csv module having default
delimiter comma(,).

Example 3: Write a python list into person.csv file


1. import csv
2.
3. csvData = [['Person', 'Age'], ['Peter', '22'], ['Jasmine', '21'], ['Sam',
'24']]
4.
5. with open('person.csv', 'w') as csvFile:
6. writer = csv.writer(csvFile)
7. writer.writerows(csvData)
8.
9. csvFile.close()

When we open the person.csv file with text editor, then it will show :
Person,Age
Peter,22
Jasmine,21
Sam,24

In the above program, we use csv.writer() function to write data from a list csvData into
a csv file person.csv.
Note: The writerow() method writes one row at a time. If you need to write all the data
at once you can use writerows() method.

CSV Files with quotes


We can write the csv file with quotes, by registering new dialects
using csv.register_dialect() class of csv module.

Example 4 : Write into person1.csv file


1. import csv
2.
3. person = [['SN', 'Person', 'DOB'],
4. ['1', 'John', '18/1/1997'],
5. ['2', 'Marie','19/2/1998'],
6. ['3', 'Simon','20/3/1999'],
7. ['4', 'Erik', '21/4/2000'],
8. ['5', 'Ana', '22/5/2001']]
9.
10. csv.register_dialect('myDialect',
11. quoting=csv.QUOTE_ALL,
12. skipinitialspace=True)
13.
14. with open('person1.csv', 'w') as f:
15. writer = csv.writer(f, dialect='myDialect')
16. for row in person:
17. writer.writerow(row)
18.
19. f.close()
When we open person1.csv file, we get following output :
"SN","Person","DOB"
"1","John","18/1/1997"
"2","Marie","19/2/1998"
"3","Simon","20/3/1999"
"4","Erik","21/4/2000"
"5","Ana","22/5/2001"

In above program, we register a dialect named myDialect. Inside myDialect we


use quoting=csv.QUOTE_ALL to write the double quote on all the values.
CSV files with Custom Delimiters
A delimiter is a string used to separate fields. The default value is comma(,).
We can write csv file having custom delimiter by registering a new dialect with the help
of csv.register_dialect().

Example 5 : Writing with custom delimiter as pipe(|)


1. import csv
2.
3. person = [['SN', 'Person', 'DOB'],
4. ['1', 'John', '18/1/1997'],
5. ['2', 'Marie','19/2/1998'],
6. ['3', 'Simon','20/3/1999'],
7. ['4', 'Erik', '21/4/2000'],
8. ['5', 'Ana', '22/5/2001']]
9.
10. csv.register_dialect('myDialect',
11. delimiter = '|',
12. quoting=csv.QUOTE_NONE,
13. skipinitialspace=True)
14.
15. with open('dob.csv', 'w') as f:
16. writer = csv.writer(f, dialect='myDialect')
17. for row in person:
18. writer.writerow(row)
19.
20. f.close()
When we open dob.csv file, we get following output:
SN|Person|DOB
1|John|18/1/1997
2|Marie|19/2/1998
3|Simon|20/3/1999
4|Erik|21/4/2000
5|Ana|22/5/2001

In the above program, we register a dialect with delimiter as pipe(|). Then we write a
list into a csv file dob.csv.

CSV File with a Lineterminator


A lineterminator is a string used to terminate lines produced by writer. The default value
is \r\n.
We can write csv file with a lineterminator in Python by registering new dialects
using csv.register_dialect() class of csv module

Example 6 : Writing csv file using a lineterminator


1. import csv
2.
3. csvData = [['Fruit', 'Quantity'], ['Apple', '5'], ['Orange', '7'], ['Mango',
'8']]
4.
5. csv.register_dialect('myDialect', delimiter = '|', lineterminator =
'\r\n\r\n')
6.
7. with open('lineterminator.csv', 'w') as f:
8. writer = csv.writer(f, dialect='myDialect')
9. writer.writerows(csvData)
10.
11. f.close()
When we open the lineterminator.csv file, we get following output:
Fruit|Quantity
Apple|5
Orange|7
Mango|8

In above code, we register a new dialects as myDialect. Then, we


use delimiter='|' where a | is considered as column separator. After that, we
use lineterminator='\r\n\r\n' where each row separates after every two lines.
Note : Python's CSV module only accepts \r\n, \n or \r as lineterminator.

CSV File with quotechars


We can write the csv file with custom quote characters, by registering new dialects
using csv.register_dialect() class of csv module.

Example 7: Writing CSV FIle with quotechars


1. import csv
2.
3. csvData = [['SN', 'Items'], ['1', 'Pen'], ['2', 'Book'], ['3', 'Copy']]
4.
5. csv.register_dialect('myDialect',
6. delimiter = '|',
7. quotechar = '"',
8. quoting=csv.QUOTE_ALL,
9. skipinitialspace=True)
10.
11. with open('quotechars.csv', 'w') as csvFile:
12. writer = csv.writer(csvFile, dialect='myDialect')
13. writer.writerows(csvData)
14.
15. print("writing completed")
16.
17. csvFile.close()

Output:
"SN"|"Items"
"1"|"Pen"
"2"|"Book"
"3"|"Copy"

In the above program, we register a dialect called myDialect. Then we use delimiter
as pipe(|) and quotechar as doublequote '"'.

Writing CSV file into a Dictionary


Using DictWriter() class of csv module, we can write a csv file into a dictionary. It
works similar to the writer() function but creates an object which maps data into a
dictionary. The keys are given by the fieldnames parameter.

Example 8: Writing dictionary into peak.csv file


1. import csv
2.
3. data = [{'mountain' : 'Everest', 'height': '8848'},
4. {'mountain' : 'K2 ', 'height': '8611'},
5. {'mountain' : 'Kanchenjunga', 'height': '8586'}]
6.
7. with open('peak.csv', 'w') as csvFile:
8. fields = ['mountain', 'height']
9. writer = csv.DictWriter(csvFile, fieldnames=fields)
10. writer.writeheader()
11. writer.writerows(data)
12.
13. print("writing completed")
14.
15. csvFile.close()
When we open peak.csv file, it will contain following output :
1. mountain,height
2. Everest,8848
3. K2,8611
4. Kangchenjunga,8586
In the above program, we use fieldnames as headings of each column in csv file. Then,
we use a DictWriter() to write dictionary data into peak.csv file.

Example 9: Writing dictionary into grade.csv file with custom


dialects
1. import csv
2.
3. csv.register_dialect('myDialect', delimiter = '|', quoting=csv.QUOTE_ALL)
4.
5. with open('grade.csv', 'w') as csvfile:
6. fieldnames = ['Name', 'Grade']
7. writer = csv.DictWriter(csvfile, fieldnames=fieldnames,
dialect="myDialect")
8. writer.writeheader()
9. writer.writerows([{'Grade': 'B', 'Name': 'Alex'},
10. {'Grade': 'A', 'Name': 'Bin'},
11. {'Grade': 'C', 'Name': 'Tom'}])
12.
13. print("writing completed")
When we open grade.csv file, it will contain following output:
"Name"|"Grade"
"Alex"|"B"
"Bin"|"A"
"Tom"|"C"

In the above program, we create a custom dialect called myDialect with pipe(|) as
delimiter. Then, use fieldnames as headings of each column in csv file. Finally, we use
a DictWriter() to write dictionary data into grade.csv file.

You might also like