由于客戶(hù)提供的是excel文件,在使用時(shí)期望使用csv文件格式,且對(duì)某些字段內(nèi)容需要做一些處理,如從某個(gè)字段中固定的幾位抽取出來(lái),獨(dú)立作為一個(gè)字段等,下面記錄下使用acaconda處理的過(guò)程;
import pandas
df = pandas.read_excel("/***/***.xlsx")
df.columns = [內(nèi)部為你給你的excel每一列自定義的名稱(chēng)](比如我給我的excel自定義列表為:
["url","productName","***",。。。,"***"])
(下面開(kāi)始你自己的表演,對(duì)每一列內(nèi)容進(jìn)行你自己需要的處理)
df["url"] = df["url"].str.replace("http", "https")
df["***"] = df["***"].str.replace("\n", " ")
df["stract_content"] = df["url"].str[-6:]
表演結(jié)束之后,就要保存了
df.drop_duplicates().fillna("").to_csv("/***/***.csv", index=False, encoding="utf-8", sep="\3")
上面為讀取路徑,下面為保存路徑
結(jié)束之后,可以通過(guò)
df.head(5)來(lái)查看結(jié)果前5個(gè),判斷處理結(jié)果是否符合你的預(yù)期即可;?
下面總結(jié)一下將List內(nèi)容存儲(chǔ)到excel和csv:
直接上代碼:
list存儲(chǔ)到csv文件:下面代碼使用codes包操作
with codecs.open("result.csv", "w", encoding="utf-8") as fw:
for i in final_res:
fw.write(u"\3".join([j if isinstance(j, unicode) else str(j).decode("utf-8") for j in i]) + "\n")
下面是使用pandas操作:
columns是字符串列表,作為表格的標(biāo)題頭
df = pandas.DataFrame(my_list)
df.columns = ["col1", "col2", ...]
df.to_excel("result.xlsx", index=False, encoding="utf-8")
也可以先存儲(chǔ)為csv文件,然后使用pandas轉(zhuǎn)化為excel:
with codecs.open("result.csv", "w", encoding="utf-8") as fw:
for i in final_res:
fw.write(u"\3".join([j if isinstance(j, unicode) else str(j).decode("utf-8") for j in i]) + "\n")
with pandas.ExcelWriter('result.xlsx') as ew:
pandas.read_csv("result.csv", sep='\3').to_excel(ew, index=False, header=["文件名", "query調(diào)用時(shí)間", "調(diào)用ip", "調(diào)用類(lèi)型", "query結(jié)束時(shí)間", "行序號(hào)", "top5 sku", "文件總耗時(shí)"], sheet_name="result", encoding="utf-8")
或者:
with codecs.open("result.csv", "w", encoding="utf-8") as fw:
for i in final_res:
fw.write(u"\3".join([j if isinstance(j, unicode) else str(j).decode("utf-8") for j in i]) + "\n")
df = pandas.read_csv("result.csv", sep="\3")
df.columns = ["col1", "col2", ...]
df.to_excel("result.xlsx", index=False, encoding="utf-8")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

