今天笔者在一个网站中抓取了一些数据,整理成一个大列表后,希望输出到excel表格文件中保存
虽然有多种模块都可以完成这个需求,但笔者这里想到了可以使用最为简单高效的pandas模块
核心代码如下(使用元组也是可以的,子元素也可以列表和元组同时使用,效果一样的):
import pandas as pd content_list=[["Instance Type","Term","Offering Type","Upfront Price(CNY)"], ["General Purpose"],["Author:qq_5201351"], ["t4g.micro","1yr","No Upfront",0], ["t4g.micro","1yr","Partial Upfront","67"], ["t4g.micro","1yr","All Upfront","3729"] ] df=pd.DataFrame(content_list) df.to_excel("./qq_5201351.xlsx")
这样默认的写出来,也会面临一个问题,就是内容的每一列,会有默认的列索引(从0开始),内容的每一行会有行索引(从0开始),如下
对于内容每一列的索引,我们可以加入 columns 参数,而内容的每一行的索引,我们可以将其值设置为False,修改后的代码如下:
import pandas as pd content_list=[["Instance Type","Term","Offering Type","Upfront Price(CNY)"], ["General Purpose"],["Author:qq_5201351"], ["t4g.micro","1yr","No Upfront",0], ["t4g.micro","1yr","Partial Upfront","67"], ["t4g.micro","1yr","All Upfront","3729"] ] df=pd.DataFrame(content_list[1:],columns=content_list[0]) df.to_excel("./qq_5201351_05.xlsx",index=False)
修改后,格式相对就比较完美了,效果如下:
尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/15583207.html