Skip to content

Commit 8ed3fdd

Browse files
Merge pull request #1 from Mahmoud-alzoubi95/CSV_files
add reading csv method
2 parents d9919d9 + 7662c09 commit 8ed3fdd

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CSV_files/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Reading and Writing a CSV File
2+
There are various ways to read a CSV file that uses either the CSV module or the pandas library.
3+
4+
- csv Module: The CSV module is one of the modules in Python which provides classes for reading and writing tabular information in CSV file format.
5+
6+
- pandas Library: The pandas library is one of the open-source Python libraries that provide high-performance, convenient data structures and data analysis tools and techniques for Python programming.
7+
8+
* **Read using csv.DictReader() class:** the CSV file is first opened using the open() method then it is read by using the DictReader class of csv module which works like a regular reader but maps the information in the CSV file into a dictionary. The very first line of the file consists of dictionary keys.
9+
10+

CSV_files/assets/addresses.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
John,Doe,120 jefferson st.,Riverside, NJ, 08075
2+
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
3+
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
4+
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
5+
,Blankman,,SomeTown, SD, 00298
6+
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

CSV_files/read_csv.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import csv
2+
3+
def read_using_DictReader():
4+
# opening the CSV file
5+
with open('CSV_files/assets/addresses.csv', mode ='r') as file:
6+
# reading the CSV file
7+
csvFile = csv.DictReader(file)
8+
9+
# displaying the contents of the CSV file
10+
for lines in csvFile:
11+
return lines
12+
13+
14+
15+
16+
if __name__=="__main__":
17+
print(read_using_DictReader())

0 commit comments

Comments
 (0)