Skip to content

Commit 0e36842

Browse files
authored
Merge pull request larymak#45 from Mahmoud-alzoubi95/main
Add "Read and write CSV files"
2 parents d9919d9 + 9c8029e commit 0e36842

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

CSV_files/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
* **Read using pandas module** : you can read cvs file using pandas ,using read_csv method , you can also determine if you want to read the head or the tail or read specific piece of data .
10+
11+
12+
* There are various classes provide by csv module for writting to csv:
13+
- using csv.writer class
14+
- using csv.DictWritter class
15+
16+
* **Write using cvs.DictWritter() class**: this class returns a writer object which maps directories onto output rows.
17+
18+
19+
* **Write using pandas module**:You can save your Pandas DataFrame as a CSV file with .to_csv(data)
20+
21+
22+
23+
24+
25+
# Steps :
26+
27+
1- clone the repo
28+
29+
2- poetry install
30+
31+
3- poetry shell
32+
33+
4- run the wanted file

CSV_files/assets/addresses.csv

Whitespace-only changes.

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/read_csv.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import csv
2+
import pandas as pd
3+
4+
def read_using_DictReader(path):
5+
# opening the CSV file
6+
with open(path, mode ='r') as file:
7+
# reading the CSV file
8+
csvFile = csv.DictReader(file)
9+
10+
# displaying the contents of the CSV file
11+
for lines in csvFile:
12+
return lines
13+
14+
def read_by_pandas_head():
15+
data=pd.read_csv('CSV_files/assets/addresses.csv')
16+
return data.head()
17+
18+
def read_by_pandas_tail():
19+
data=pd.read_csv('CSV_files/assets/addresses.csv')
20+
return data.tail()
21+
22+
if __name__=="__main__":
23+
print(read_using_DictReader('assets/addresses.csv'))
24+
print(read_using_DictReader())
25+
print(read_by_pandas_head())
26+
print(read_by_pandas_tail())
27+

CSV_files/write_csv.py

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

0 commit comments

Comments
 (0)