cathw commited on
Commit
b652229
1 Parent(s): 49b1ead

Delete CSVtransformer.py

Browse files
Files changed (1) hide show
  1. CSVtransformer.py +0 -68
CSVtransformer.py DELETED
@@ -1,68 +0,0 @@
1
- import csv
2
- import pandas as pd
3
-
4
- class CSVtoJSONTransformer:
5
- def __init__(self, csv_reader):
6
- self.csv_reader = csv_reader
7
-
8
- def transform_to_json(self):
9
- transformed_data = []
10
- next(self.csv_reader) # Skip header if present
11
- df = self.csv_reader
12
- df["Author"] = df["Author"].replace({pd.NA: None})
13
- df["Comment Body"] = df["Comment Body"].replace({pd.NA: None})
14
-
15
- # Define an empty list to store the transformed data
16
- transformed_data = []
17
-
18
- # Group the DataFrame by the 'Subreddit' column
19
- grouped_df = df.groupby('Subreddit')
20
-
21
- # Iterate through each subreddit group
22
- for subreddit, group in grouped_df:
23
- subreddit_data = {
24
- "Subreddit": subreddit,
25
- "Posts": []
26
- }
27
-
28
- # Iterate through each row in the subreddit group
29
- for index, row in group.iterrows():
30
- post_title = row['Post Title']
31
- comment_body = row['Comment Body']
32
- timestamp = row['Timestamp']
33
- upvotes = row['Upvotes']
34
- ID = row['ID']
35
- author = row['Author']
36
- replies = row['Number of Replies']
37
-
38
- # Check if the post title already exists under the subreddit
39
- post_exists = False
40
- for post in subreddit_data["Posts"]:
41
- if post['PostTitle'] == post_title:
42
- post_exists = True
43
- post['Comments'].append({
44
- "CommentID": ID,
45
- "Author": author,
46
- "CommentBody": comment_body,
47
- "Timestamp": timestamp,
48
- "Upvotes": upvotes,
49
- "NumberofReplies": replies
50
- })
51
- break
52
-
53
- # If the post title does not exist, create a new entry
54
- if not post_exists:
55
- subreddit_data["Posts"].append({
56
- "PostID": len(subreddit_data["Posts"]) + 1,
57
- "PostTitle": post_title,
58
- "Comments": [{
59
- "CommentID": ID,
60
- "Author": author,
61
- "CommentBody": comment_body,
62
- "Timestamp": timestamp,
63
- "Upvotes": upvotes,
64
- "NumberofReplies": replies
65
- }]
66
- })
67
-
68
- return transformed_data