File size: 979 Bytes
e0e5b16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import csv
# Open sexting_conversations.txt and read all lines
with open('sexting_conversations.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
# Initialize variables
data = []
text = ""
char_count = 0
# Loop through each line in the file
for line in lines:
# Increase character count
char_count += len(line)
# Add line to text
text += line
# 92 is the avg character length per line multiplied by 2
if char_count >= 92:
# Add text to data array with is_toxic value of 1
data.append([text.strip(), 1])
# Reset text and character count
text = ""
char_count = 0
# If there is any remaining text, add it to data array with is_toxic value of 1
if text:
data.append([text.strip(), 1])
# Save data array to CSV with column "text,is_toxic"
with open('output.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['text', 'is_toxic'])
writer.writerows(data)
|