-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_utils.py
30 lines (25 loc) · 1.03 KB
/
csv_utils.py
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
"""Utility functions for handling CSV operations."""
import os
import csv
from reddit_search import RedditPost
def save_posts_to_csv(posts: list[RedditPost], comments: list[str], filename: str) -> str:
"""
Save the relevant posts and generated comments to a CSV file.
:param posts: List of relevant Reddit submissions.
:param comments: List of generated comments.
:param filename: The filename of the CSV file.
:return: The path to the saved CSV file.
"""
os.makedirs("output", exist_ok=True)
filepath = os.path.join("output", filename)
with open(filepath, "w", newline="", encoding="utf-8") as csvfile:
fieldnames = ["Post Title", "Post URL", "Generated Comment"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for post, comment in zip(posts, comments):
writer.writerow({
"Post Title": post.title,
"Post URL": post.url,
"Generated Comment": comment
})
return filepath