Created
March 4, 2024 11:17
-
-
Save remorses/897e3db104f62ce6591c8f7757c48764 to your computer and use it in GitHub Desktop.
Convert Openai ChatGPT exported data to csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import json | |
def extract_messages(data, max_messages): | |
messages = [] | |
for message_id, message_data in data["mapping"].items(): | |
if message_data["message"]: | |
message = message_data["message"] | |
content = message.get("content", {}).get("parts", [""])[0] | |
messages.append(content) | |
return messages[:max_messages] | |
def convert_to_csv(data, filename="output.csv", max_messages=10): | |
with open(filename, "w", newline="") as csvfile: | |
writer = csv.writer(csvfile) | |
writer.writerow( | |
[ | |
"title", | |
"id", | |
# add message_n columns | |
*[f"message_{i}" for i in range(max_messages)], | |
] | |
) | |
for item in data: | |
messages = extract_messages(item, max_messages) | |
writer.writerow([item["title"], item["id"]] + messages) | |
json_data_string = open( | |
"/Users/morse/Downloads/ae5f770c2cd77e9887c5b89b78a41fbab359a7488e1f762dc14e40c9b9ba4f20-2024-03-03-21-17-42/conversations.json" | |
).read() | |
# Load JSON data | |
data = json.loads(json_data_string) # Replace with your actual JSON data | |
filename = "output.csv" | |
# Convert to CSV | |
convert_to_csv(data, filename) | |
print(f"Converted data written to CSV file: {filename}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment