Search code examples
pythongmailimaplib

how to replace spaces with commas in a tuple?


I need to get a count of the emails received daily in a Gmail mailbox and save that count in a table. To do this I am using the imaplib:

import imaplib  
import re
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)  
obj.login('[email protected]', 'password')  
obj.select('Inbox')     
('OK', ['50'])  
gmail_count = obj.search(None,'(SINCE "01-Sep-2020" BEFORE "02-Sep-2020")') 
print(gmail_count)

and I get something like this: ('OK', [b'28410 28411 28412 28413 28414 28415 28416 28417 28418 28419 28420 28421 28422 28423 28424 28425 28426 28427 28428 28429 28430 28431 28432 28433 28434 28435 28436 28437 28438 28439'])

I now need to count how many values are in this tuple to get the number of emails so I would like to replace the spaces with commas. How Can I do this?


Solution

  • You can use the replace built-in function to replace spaces by comas in a string

    In your case you firstly have to convert your bytes array in a string with the decode("utf-8") function.

    If your data are not utf-8 encoded you can change the parameter in the decode function with the correct encoding method.

    values = gmail_count[1][0]
    replaced = values.decode("utf-8").replace(" ", ",")