I have a file of sms messages in plain text that I want to convert to a CSV file. The format is as follows:
Sent on 1/1/2023 7:30:33 AM to Person1
Message
-----
Received on 5/20/2023 4:55:33 PM from Person1
Message
I want to loop through the text file, retrieve the lines and create a result like the one below.
Status | Date | Contact | Message |
---|---|---|---|
Sent | 1/1/2023 7:30:33 AM | Person1 | Message |
Received | 5/20/2023 4:55:33 PM | Person1 | Message |
I started with the code below, but I'm pretty new to Python and can't figure out how to transpose the lines into columns.
import csv
import openpyxl
input_file = 'output.txt'
output_file = 'allmessages.csv'
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
with open(input_file, 'r') as data:
reader = csv.reader(data, delimiter='\t')
for row in reader:
ws.append(row)
wb.save(output_file)
Any suggestions would be greatly appreciated!