Skip to content

Commit e6f3b58

Browse files
committed
Added cross-platform compatibility for data.csv
1 parent 61ffb23 commit e6f3b58

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

data/processed/city_report.csv

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
nombre,apellido,ciudad,pais,edad,carrera
2-
Daniel,Brown,Lima,Peru,23,Education
3-
Liam,Taylor,Lima,Peru,21,Computer Engineering
4-
Ethan,Lee,Lima,Peru,23,Education
5-
Chloe,Allen,Lima,Peru,27,Education
6-
Sophia,Hernandez,Lima,Peru,28,Computer Engineering
7-
Sofia,Clark,Lima,Peru,31,Psychology
8-
Mia,Hall,Lima,Peru,30,Medicine
2+
David,Williams,Medellín,Colombia,30,Medicine
3+
Isabella,Anderson,Medellín,Colombia,31,Education
4+
Natalie,Lopez,Medellín,Colombia,32,Medicine
5+
Daniel,Rodriguez,Medellín,Colombia,40,Education
6+
Mateo,Jackson,Medellín,Colombia,19,Computer Engineering
7+
James,Lee,Medellín,Colombia,21,Psychology

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def main() -> None:
3131
:return: None
3232
:rtype: NoneType
3333
"""
34-
student: Student = Student("data/raw/data.csv")
34+
student: Student = Student()
3535
students: list[dict[str, Any]] = student.load_data()
3636
while True:
3737
print_menu()

processing/student.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
class Student:
1717
"""A class to represent the Student data."""
1818

19-
def __init__(self, csv_file: str):
19+
def __init__(self, csv_file: str = "data.csv"):
2020
"""
2121
Constructs all the necessary attributes for the Student object.
22-
:param csv_file: path to the csv file
22+
:param csv_file: The name to the csv file. The default is
23+
data.csv
2324
:type csv_file: str
2425
"""
2526
self.csv_file: str = csv_file
@@ -35,20 +36,24 @@ def load_data(self) -> list[dict[str, Any]]:
3536
students: list[dict[str, Any]] = []
3637
file_path: str = os.path.join(
3738
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
38-
self.csv_file.replace("/", "\\"))
39-
with open(file_path, "r", encoding=ENCODING) as file:
40-
reader = csv.DictReader(file)
41-
student: dict[str, Any]
42-
for student in reader:
43-
try:
44-
age: int = int(student["edad"])
45-
if age > 0:
46-
student["edad"] = age
47-
students.append(student)
48-
else:
49-
logger.error(VALID_AGE, age)
50-
raise ValidationError(VALID_AGE)
51-
except ValueError as exc:
52-
logger.error(NOT_NUMBER)
53-
raise ValidationError(NOT_NUMBER) from exc
39+
"data", "raw", self.csv_file)
40+
try:
41+
with open(file_path, "r", encoding=ENCODING) as file:
42+
reader = csv.DictReader(file)
43+
student: dict[str, Any]
44+
for student in reader:
45+
try:
46+
age: int = int(student["edad"])
47+
if age > 0:
48+
student["edad"] = age
49+
students.append(student)
50+
else:
51+
logger.error(VALID_AGE, age)
52+
raise ValidationError(VALID_AGE)
53+
except ValueError as exc:
54+
logger.error(NOT_NUMBER)
55+
raise ValidationError(NOT_NUMBER) from exc
56+
except FileNotFoundError:
57+
logger.error("Data file not found at %s. Please check the"
58+
" location of your data file.", file_path)
5459
return students

0 commit comments

Comments
 (0)