0

for some reason i keep getting the TypeError at this

TypeError Traceback (most recent call last) <

ipython-input-19-3490eb36442d> in <module>
      2 result, numbers = mail.uid('search', None, "ALL")
      3 uids = numbers[0].split()
----> 4 result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')

mail.select("INBOX")
result, numbers = mail.uid('search', None, "ALL")
uids = numbers[0].split()
result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')
date_list = []
from_list = []
message_text = []
for _, message in messages[::2]:
    msg = email.message_from_string(message)
    if msg.is_multipart():
        t = []
    for p in msg.get_payload():
        t.append(p.get_payload(decode=True))
        message_text.append(t[0])

    else:message_text.append(msg.get_payload(decode=True))
    date_list.append(msg.get('date'))
    from_list.append(msg.get('from'))
    date_list = pd.to_datetime(date_list)
    print (len(message_text))
    print (len(from_list))
    df = pd.DataFrame(data={'Date':date_list,'Sender':from_list,'Message':message_text})
    print (df.head())
    df.to_csv('~inbox_email.csv',index=False)
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
SuperLmnt
  • 1
  • 1
  • 1

1 Answers1

0

This line

result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')

is raising the exception

TypeError sequence item 0: expected str instance, bytes found

Inspecting the line, 'fetch' and '(BODY[])' are already strings, so they are unlikely to be the problem.

That leaves ','.join(uids). uids is actually a list of bytes instances, so str.join is raising the exception because it expects an iterable of str instances.

To fix the problem, decode numbers[0] to str before manipulating it.

result, numbers = mail.uid('search', None, "ALL")
uids = numbers[0].decode().split()    # <- decode before splitting
result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153