简体   繁体   English

如何 append 将 while 循环中的列表添加到 python - selenium 中的同一列表

[英]how to append a list in a while loop to the same list in python - selenium

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.简而言之,我的问题是我想从“n”个邮箱和 append 邮箱(及其消息)一个接一个地获取接收到的消息,然后我想作为数据帧打印到 excel 文件中. (eg n =5) (例如 n =5)

mailbox_list = ['mailbox_1','mailbox_2','mailbox_3','mailbox_4','mailbox_5']邮箱列表 = ['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?有人可以帮我吗?我怎么能 append 将 msg_list 到 while 循环中的下一个项目(下一个 msg_list)?

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.您可以定义并保留一些全局变量,让我们将其称为循环外的total_msg_list和 append 循环中的当前msg_list到该全局变量。
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM