程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将数据从 Python 导出到 Excel 表格大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决将数据从 Python 导出到 Excel 表格?

开发过程中遇到将数据从 Python 导出到 Excel 表格的问题如何解决?下面主要结合日常开发的经验,给出你关于将数据从 Python 导出到 Excel 表格的解决方法建议,希望对你解决将数据从 Python 导出到 Excel 表格有所启发或帮助;

经过多次研究,我无法将抓取的数据正确导出到 Excel 表中...

这是我的代码:

from SELEnium import webdriver
from SELEnium.webdriver.common.keys import Keys 
from SELEnium.webdriver import ActionChains
import time 
from time import sleep
import pandas as pd
import csv
from bs4 import BeautifulSoup
from SELEnium.webdriver.Chrome.options import Options


PATH = "C:\Program files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get('https://www.xxx')
time.sleep(16)

boutons = driver.find_elements_by_class_name('fiche-detail')
for x in boutons:
        if x != boutons[0]:
            x.click()
            time.sleep(0.10)
        else:
            conTinue

cabinets = driver.find_elements_by_class_name("fiche")
for cabinet in cabinets:
    print(cabinet.text)

codePostaux = driver.find_elements_by_class_name("fiche-info ville-codepostal")
for codePostal in codePostaux:
    print(codePostal.text)

mails = driver.find_elements_by_class_name("fiche-info email")
for mail in mails:
    print(mail.text)

rues = driver.find_elements_by_class_name("fiche-info rue") 
for rue in rues:
    print(rue.text)

df.to_excel('EXTRACTION.xLSX',index = falsE)

我想让它们按列 ['Cabinets'],['mails'],['codePostaux'] 组织,这些类名的数据结果将位于此列下(以便在).

编辑: 经过一些尝试并阅读有关解决数据框的文档后,我尝试查看发生了什么,我得到了这个:

"ValueError: 传递值的形状是 (3,5),索引意味着 (4,5)"

我忘了确切地说我已经抓取了数千个值(公司名称、电子邮件、街道名称、城市名称等),因此我无法指定需要在列中的值的名称。我在例子中写了“第一个值”,“第二个值”,但我认为我错了。

例如,我在我的 cmd.exe 中得到:

内阁 rue de la chèvre qui danse 75015 巴黎 contact@restaurantdelarto.com

我希望“Cabinet des toubibs”进入列[cabinets],“rue de la chèvre qui danse”进入列[rueCabinet],等等...

同样“Cabinet des toubibs”是print(cabinet.text)的结果,“rue de la chèvre qui danse”是print(rue.text)的结果

columns = {'cabinets':  ['First value','Second value',...],'mails': ['First value','codePostaux' : ['First value','Second value'],'villeCabinet' : ['First value','rueCabinet' : ['First value','Second value']
        }

df = pd.DataFrame(columns,columns = ['cabinets','mails','codePostaux','villeCabinet','rueCabinet'],index=['Cab_1','Cab_2','Cab_3','Cab_4'])

df.to_excel('EXTRACTION.xLSX',index = falsE)

提前感谢您的时间;)

P.S : 对不起,如果我犯了一些错误,英语不是我的母语

解决方法

听起来你想使用pandas,阅读有关如何创建数据框的信息,将列添加到你保存的数据中 然后在你组织好你的数据框之后,你可以使用一个名为 :

的命令
nameofdataframe.to_csv("nameforoutput.csv")
,

使用这样的代码:

cols = ['Cabinets','Mails','codePostaux']
df = df[cols]

这将重新排列您的列。

保重

编辑:刚刚看到您没有设置 DataFrame。查看如何使用 Pandas 创建 DF:https://datatofish.com/create-pandas-dataframe/

大佬总结

以上是大佬教程为你收集整理的将数据从 Python 导出到 Excel 表格全部内容,希望文章能够帮你解决将数据从 Python 导出到 Excel 表格所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。