Gustavo Molina
Gustavo Molina

Reputation: 85

transform Python returns to Excel

I have to create an Excel file with all information of agencies of Correios in Brazil, how can I create an Excel file with the values that I print in the last else, which have:

else:
lista_valores.append(text_info)
print(text_info)

I've removed all imports cause stackoverflow says the post's mostly code.

My code:

FOLDER = r'D:\Users\gbenites\Desktop\Inovação\Correiosx'
chrome_path = r"D:\Users\gbenites\Desktop\Inovação\arquivos py\WebDriver\chromedriver.exe"
url = 'http://www2.correios.com.br/sistemas/agencias/'


driver = webdriver.Chrome(chrome_path)
driver.get("http://www2.correios.com.br/sistemas/agencias/")

file = open(FOLDER + 'Correios.csv', 'a', newline = '', encoding = 'utf-8')

#WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'municipioAgencia')))
driver.maximize_window()

lista_valores = []

time.sleep(3) 
estadoList = Select(driver.find_element_by_name('estadoAgencia'))


for index in range(1,len(estadoList.options)):
    select = Select(driver.find_element_by_name('estadoAgencia'))
    print("selecting Estado: ",select.options[index].text)       
    select.select_by_index(index)
    time.sleep(2)

    municípioList = Select(driver.find_element_by_name('municipioAgencia'))
    for index_b in range(1,len(municípioList.options)):
        select_b = Select(driver.find_element_by_name('municipioAgencia'))
        print("...selecting Municipio",select_b.options[index_b].text.replace("'",""))
        select_b.select_by_index(index_b)
        time.sleep(3)

        bairroList = Select(driver.find_element_by_name('bairroAgencia')) 
        for index_c in range(1,len(bairroslist)) 
            select_c = Select(driver.find_element_by_name('bairroAgencia'))
            print('...selecting Bairro',select_c.options[index_c].text.replace("'",""))
            select_c.select_by_index(index_c)
            time.sleep(3)

            #get_info_list =  WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//table[@class = 'dadosAgencia']//td")))
            info_list = driver.find_elements_by_xpath("//table[@class = 'dadosAgencia']//span")
            td_list = driver.find_elements_by_xpath("//table[@class = 'dadosAgencia']//td")
            get_table_list = driver.find_elements_by_xpath("//table[@class = 'dadosAgencia']")


            for data in range(0,len(get_table_list)):
                for record in range(0,len(td_list)):
                        text_info = td_list[record].get_attribute('innerText') 
                        if text_info in lista_valores:
                            pass
                        else:
                            lista_valores.append(text_info)
                            print(text_info)

Upvotes: 0

Views: 40

Answers (1)

Jack Walsh
Jack Walsh

Reputation: 582

So if I'm not mistaken, you want to turn lista_valores to a Excel file.

The pandas library is a reputable way to manipulate data in the form of a DataFrame.

If you place import pandas as pd to your imports, add the following code to create an exel sheet from lista_valores:

result = pd.DataFrame(lista_valores)
result.to_csv('lista_valores.csv')

Without knowing the data type of lista_valores, as well as the data type of text_info, it is hard to understand the question. Please reword or further explain your question if this solution does not help.

Upvotes: 1

Related Questions