Skip to content

Commit 02dd0f5

Browse files
authored
Update csvparser.py
Formatted the file
1 parent 3d7c887 commit 02dd0f5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csv
22
from typing import IO, AsyncGenerator
3+
34
from .page import Page
45
from .parser import Parser
56

@@ -10,11 +11,20 @@ class CsvParser(Parser):
1011
"""
1112

1213
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
14+
# Check if content is in bytes (binary file) and decode to string
15+
if isinstance(content, (bytes, bytearray)):
16+
content = content.decode("utf-8")
17+
elif hasattr(content, "read"): # Handle BufferedReader
18+
content = content.read().decode("utf-8")
19+
20+
# Create a CSV reader from the text content
21+
reader = csv.reader(content.splitlines())
1622
offset = 0
23+
24+
# Skip the header row
25+
next(reader, None)
26+
1727
for i, row in enumerate(reader):
18-
page_text = ",".join(row) # Combine CSV row elements back to a string
28+
page_text = ",".join(row)
1929
yield Page(i, offset, page_text)
20-
offset += len(page_text) + 1 # Add 1 for the newline character or comma
30+
offset += len(page_text) + 1 # Account for newline character

0 commit comments

Comments
 (0)