Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #924: use csv.DictReader to read csv file #926

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions agenta-backend/agenta_backend/routers/testset_router.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import os
import csv
import json
Expand Down Expand Up @@ -88,15 +89,14 @@ async def upload_file(
# Read and parse the CSV file
csv_data = await file.read()
csv_text = csv_data.decode("utf-8")
csv_reader = csv.reader(csv_text.splitlines())
columns = next(csv_reader) # Get the column names

# Populate the document with column names and values
# Use StringIO to create a file-like object from the string
csv_file_like_object = io.StringIO(csv_text)
csv_reader = csv.DictReader(csv_file_like_object)

# Populate the document with rows from the CSV reader
for row in csv_reader:
row_data = {}
for i, value in enumerate(row):
row_data[columns[i]] = value
document["csvdata"].append(row_data)
document["csvdata"].append(row)

user = await get_user(user_uid=user_org_data["uid"])
testset_instance = TestSetDB(**document, user=user)
Expand Down
Loading