-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathreaders.py
More file actions
37 lines (28 loc) · 932 Bytes
/
readers.py
File metadata and controls
37 lines (28 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import csv
import json
from itertools import batched # Python >= 3.12
class TextReader:
def __init__(self, filename):
self.filename = filename
def read(self):
with open(self.filename, encoding="utf-8") as file:
return [
{
"name": batch[0].strip(),
"age": batch[1].strip(),
"job": batch[2].strip(),
}
for batch in batched(file.readlines(), 3)
]
class CSVReader:
def __init__(self, filename):
self.filename = filename
def read(self):
with open(self.filename, encoding="utf-8", newline="") as file:
return list(csv.DictReader(file))
class JSONReader:
def __init__(self, filename):
self.filename = filename
def read(self):
with open(self.filename, encoding="utf-8") as file:
return json.load(file)