Skip to content

Commit aeae507

Browse files
Merge pull request #2 from Mahmoud-alzoubi95/write_on_csv_files
add write to csv file feature
2 parents 8ed3fdd + 9d4fc0e commit aeae507

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

CSV_files/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# 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.
2+
* There are various ways to read a CSV file that uses either the CSV module or the pandas library.
33

44
- 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.
55

66
- 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.
77

88
* **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.
99

10+
* There are various classes provide by csv module for writting to csv:
11+
- using csv.writer class
12+
- using csv.DictWritter class
1013

14+
* **Write using cvs.DictWritter() class**: this class returns a writer object which maps directories onto output rows.
15+

CSV_files/assets/addresses.csv

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
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/assets/course_name.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name,course
2+
Noura,python40python401
3+
Mahmoud,python401
4+
Nizar,python401
5+
Raneem,python401
6+
Omer,python401

CSV_files/write_csv.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import csv
2+
3+
4+
def write_to_csv_files_using_DictWriter_class(data,fields,filename):
5+
6+
with open(filename, 'w') as csvfile:
7+
# creating a csv dict writer object
8+
writer = csv.DictWriter(csvfile, fieldnames = fields)
9+
10+
# writing headers (field names)
11+
writer.writeheader()
12+
13+
# writing data rows
14+
writer.writerows(data)
15+
16+
17+
18+
if __name__=="__main__":
19+
# my data rows as dictionary objects
20+
mydata =[{'name': 'Noura', 'course': 'python40python401'},
21+
{'name': 'Mahmoud', 'course': 'python401'},
22+
{'name': 'Nizar', 'course': 'python401'},
23+
{'name': 'Raneem', 'course': 'python401'},
24+
{'name': 'Omer', 'course': 'python401'}, ]
25+
26+
# field names
27+
fields = ['name','course']
28+
29+
# name of csv file
30+
filename = "CSV_files/assets/course_name.csv"
31+
32+
# writing to csv file
33+
print(write_to_csv_files_using_DictWriter_class(mydata,fields,filename))

0 commit comments

Comments
 (0)