File size: 861 Bytes
499003f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 pandas as pd

special = []
df = pd.read_csv('spam_data.csv')

for index, row in df.iterrows():
    text = row['text']

    if text is not None and text != '' and 'Subject: ' in text:
        subject = text.split("""
Subject: """)[1]
        
        if '\n' in subject:
            subject = subject.split("\n")[0].strip()
        elif '\r' in subject:
            subject = subject.split("\r")[0].strip()
        else:
            raise Exception('No newline found in subject: ' + str(text))
        content = text.split("""

""")[1:]
        content = """

""".join(content)
        
        text = subject + """

""" + content
        
        df.at[index, 'text'] = text
    else:
        special.append({'text': text, 'is_spam': row['is_spam']})

df.to_csv('spam_data_clean.csv', index=False)
pd.DataFrame(special).to_csv('special.csv', index=False)