Skip to content

Commit bc1b5e3

Browse files
authored
Create csvparser.py
I am adding the CSV parser python code. it works with basic CSV files.
1 parent 0225f75 commit bc1b5e3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import csv
2+
from typing import IO, AsyncGenerator
3+
from .page import Page
4+
from .parser import Parser
5+
6+
7+
class CsvParser(Parser):
8+
"""
9+
Concrete parser that can parse CSV into Page objects. Each row becomes a Page object.
10+
"""
11+
12+
async def parse(self, content: IO) -> AsyncGenerator[Page, None]:
13+
# Ensure the file is read in text mode
14+
text_content = content.read().decode('utf-8') # Decode bytes to string if opened in binary mode
15+
reader = csv.reader(text_content.splitlines()) # Create CSV reader from text lines
16+
offset = 0
17+
for i, row in enumerate(reader):
18+
page_text = ",".join(row) # Combine CSV row elements back to a string
19+
yield Page(i, offset, page_text)
20+
offset += len(page_text) + 1 # Add 1 for the newline character or comma

0 commit comments

Comments
 (0)