Skip to content

Commit ad9e5db

Browse files
update on CSV_files
let the function accept arguments , tuned read_using_DictReader func
1 parent aa8bc55 commit ad9e5db

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

CSV_files/assets/addresses.csv

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name,addresses
2+
Noura,Street 1/House 1
3+
Mahmoud,Street 1/House 2
4+
Nizar,Street 1/House 3
5+
Raneem,Street 1/House 4
6+
Omer,Street 1/House 5
7+
Mark,Street 1/House 6
8+
John,Street 1/House 7
9+
Tim,Street 1/House 8
10+
Sara,Street 1/House 9
11+
Rob,Street 1/House 10
12+
Keanu,Street 1/House 11

CSV_files/read_csv.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@ def read_using_DictReader(path):
66
with open(path, mode ='r') as file:
77
# reading the CSV file
88
csvFile = csv.DictReader(file)
9-
9+
main_dic=[]
1010
# displaying the contents of the CSV file
1111
for lines in csvFile:
12-
return lines
12+
main_dic.append(lines)
13+
return main_dic
1314

14-
def read_by_pandas_head():
15-
data=pd.read_csv('CSV_files/assets/addresses.csv')
15+
#retrun the top 5 rows in CSV file
16+
def read_by_pandas_head(path):
17+
data=pd.read_csv(path)
1618
return data.head()
1719

18-
def read_by_pandas_tail():
19-
data=pd.read_csv('CSV_files/assets/addresses.csv')
20+
21+
#retrun the bottom 5 rows in CSV file
22+
def read_by_pandas_tail(path):
23+
data=pd.read_csv(path)
2024
return data.tail()
2125

2226
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+
28+
path='./assets/addresses.csv'
29+
print(read_using_DictReader(path))
30+
print(read_by_pandas_head(path))
31+
print(read_by_pandas_tail(path))
2732

0 commit comments

Comments
 (0)