Jun-30-2022, 07:11 PM
(Jun-30-2022, 06:58 PM)deanhystad Wrote: You are making a 1 column dataframe of strings. If you made a dataframe that had columns for date, time, sender and content you would get a proper csv file from df.to_csv(). Here's a short example:
mport pandas as pd from random import choice import time, datetime senders = ["Me", "You", "Dog named Boo"] content = [ "Just wanted to sa Hi!", "Coming home soon.", "Take me for a walk.", "We're out of dog food.", None, ] messages = [] for _ in range(10): timestamp = datetime.datetime.now() messages.append( [ timestamp.strftime("%m/%d/%Y"), timestamp.strftime("%I:%M:%S %p"), choice(senders), choice(content), ] ) time.sleep(1) df = pd.DataFrame(messages, columns=["Date", "Time", "Sender", "Content"]) print(df) df.to_csv("test.csv")The dataframe looks like this:
And the generated CSV file like this.
Output:Date Time Sender Content 0 06/30/2022 01:57:50 PM Dog named Boo None 1 06/30/2022 01:57:51 PM You Just wanted to sa Hi! 2 06/30/2022 01:57:52 PM You Coming home soon. 3 06/30/2022 01:57:53 PM Dog named Boo Coming home soon. 4 06/30/2022 01:57:54 PM You None 5 06/30/2022 01:57:55 PM You Take me for a walk. 6 06/30/2022 01:57:56 PM Dog named Boo None 7 06/30/2022 01:57:57 PM You Take me for a walk. 8 06/30/2022 01:57:58 PM Me We're out of dog food. 9 06/30/2022 01:58:00 PM Me Just wanted to sa Hi!
If I want to remove the column or row headers this is easily accomplished using arguments in the to_csv() call.
Output:,Date,Time,Sender,Content 0,06/30/2022,01:57:50 PM,Dog named Boo, 1,06/30/2022,01:57:51 PM,You,Just wanted to sa Hi! 2,06/30/2022,01:57:52 PM,You,Coming home soon. 3,06/30/2022,01:57:53 PM,Dog named Boo,Coming home soon. 4,06/30/2022,01:57:54 PM,You, 5,06/30/2022,01:57:55 PM,You,Take me for a walk. 6,06/30/2022,01:57:56 PM,Dog named Boo, 7,06/30/2022,01:57:57 PM,You,Take me for a walk. 8,06/30/2022,01:57:58 PM,Me,We're out of dog food. 9,06/30/2022,01:58:00 PM,Me,Just wanted to sa Hi!
that suggestion helped and corrected it, thank you
here is what i changed my code to:
import json from datetime import datetime import pandas as pd import os import csv f = open('message_1.json') data = json.load(f) lv = [] for message in data["messages"]: timestamp = datetime.fromtimestamp(message["timestamp_ms"] / 1000) if 'content' not in message: lv.append( [ timestamp.strftime("%m/%d/%Y"), timestamp.strftime("%I:%M:%S %p"), message["sender_name"], "" ]) else: lv.append( [ timestamp.strftime("%m/%d/%Y"), timestamp.strftime("%I:%M:%S %p"), message["sender_name"], message["content"] ]) df = pd.DataFrame(lv, columns=["Date", "Time", "Sender", "Content"]) df.to_csv('igMess.csv', header=None, index=None, quoting=csv.QUOTE_NONE, escapechar=",", mode='a') f.close()