cathw commited on
Commit
82ae337
1 Parent(s): 125d169

Delete csvtransformerjson.py

Browse files
Files changed (1) hide show
  1. csvtransformerjson.py +0 -70
csvtransformerjson.py DELETED
@@ -1,70 +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
- df = pd.DataFrame(self.csv_reader) # Convert csv_reader to DataFrame
10
- df["Author"] = df["Author"].replace({pd.NA: None})
11
- df["Comment Body"] = df["Comment Body"].replace({pd.NA: None})
12
-
13
- transformed_data = []
14
-
15
- # Group the DataFrame by the 'Subreddit' column
16
- grouped_df = df.groupby('Subreddit')
17
-
18
- for subreddit, group in grouped_df:
19
- subreddit_data = {
20
- "Subreddit": subreddit,
21
- "Posts": []
22
- }
23
-
24
- # Dictionary to keep track of post titles and their corresponding post IDs
25
- post_title_to_id = {}
26
-
27
- for index, row in group.iterrows():
28
- post_title = row['Post Title']
29
- comment_body = row['Comment Body']
30
- timestamp = row['Timestamp']
31
- upvotes = row['Upvotes']
32
- ID = row['ID']
33
- author = row['Author']
34
- replies = row['Number of Replies']
35
-
36
- # Check if the post title already exists under the subreddit
37
- if post_title in post_title_to_id:
38
- post_id = post_title_to_id[post_title]
39
- # Append comment to existing post
40
- for post in subreddit_data["Posts"]:
41
- if post['PostID'] == post_id:
42
- post['Comments'].append({
43
- "CommentID": ID,
44
- "Author": author,
45
- "CommentBody": comment_body,
46
- "Timestamp": timestamp,
47
- "Upvotes": upvotes,
48
- "NumberofReplies": replies
49
- })
50
- break
51
- else:
52
- # If the post title does not exist, create a new entry
53
- post_id = len(subreddit_data["Posts"]) + 1
54
- post_title_to_id[post_title] = post_id
55
- subreddit_data["Posts"].append({
56
- "PostID": post_id,
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
- transformed_data.append(subreddit_data)
69
-
70
- return transformed_data