Briefly, my problem is I want to get the received messages from 'n' mailboxes and append the mailboxes (with its messages) to each other one-by-one, then I want to print out as a data-frame into an excel file. (eg n =5)
mailbox_list = ['mailbox_1','mailbox_2','mailbox_3','mailbox_4','mailbox_5']
Here is my code which works for one mailbox's content:
i = 0 # starting number of mailboxes
while i < len(mailbox_list): # I want to get messages for all mailboxes one by one
try:
# get the message content
rm_content = driver.find_element(By.XPATH, '//*[@id="content"]/messages/div[2]/div/div/table')
time.sleep(1)
messages = (rm_content.text.split('\n')) # these are the messages
print('Messages found')
# add these messages to a list
msg_list = [messages[i:i + 3] for i in range(2, len(messages), 3)]
# (!!!) this list contains the messages for 1 mailbox that I want to append to the next item in the while loop
# use a dataframe for messages
df_msg_list = pd.DataFrame(msg_list)
df_msg_list.columns = ['Sender', 'Content', 'Date']
df_msg_list[['Inbox', 'Query_Date']] = pd.DataFrame([[get_key(f'{mailbox_list[i]}'), date]], index=df_msg_list.index)
print(df_msg_list)
# write into excel
writer = pd.ExcelWriter(messages_excel, engine='xlsxwriter')
df_msg_list.to_excel(writer, sheet_name=f'{date[0:10]}', header=True, index=False)
writer.save()
except NoSuchElementException:
pass # exception handling
print('No messages found')
i=i+1
Could somebody please help me how could I append the msg_list to the next item (next msg_list) in the while loop?
You can define and keep some global variable, let's call it total_msg_list
out of the loop and append the current msg_list
in the loop to that global variable.
Something like this:
total_msg_list = []
i = 0 # starting number of mailboxes
while i < len(mailbox_list): # I want to get messages for all mailboxes one by one
try:
# get the message content
rm_content = driver.find_element(By.XPATH, '//*[@id="content"]/messages/div[2]/div/div/table')
time.sleep(1)
messages = (rm_content.text.split('\n')) # these are the messages
print('Messages found')
# add these messages to a list
msg_list = [messages[i:i + 3] for i in range(2, len(messages), 3)]
total_msg_list.append(msg_list)
# use a dataframe for messages
df_msg_list = pd.DataFrame(msg_list)
df_msg_list.columns = ['Sender', 'Content', 'Date']
df_msg_list[['Inbox', 'Query_Date']] = pd.DataFrame([[get_key(f'{mailbox_list[i]}'), date]], index=df_msg_list.index)
print(df_msg_list)
# write into excel
writer = pd.ExcelWriter(messages_excel, engine='xlsxwriter')
df_msg_list.to_excel(writer, sheet_name=f'{date[0:10]}', header=True, index=False)
writer.save()
except NoSuchElementException:
pass # exception handling
print('No messages found')
i=i+1
#now, after the loop completed you can see the final content of total_msg_list
print(total_msg_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.