創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!

小編給大家分享一下保存python程序所生產(chǎn)數(shù)據(jù)的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
保存python程序生產(chǎn)數(shù)據(jù)的方法:
open函數(shù)保存
使用with open()新建對(duì)象
寫入數(shù)據(jù)(這里使用的是爬取豆瓣讀書中一本書的豆瓣短評(píng)作為例子)
import requests
from lxml import etree
#發(fā)送Request請(qǐng)求
url = 'https://book.douban.com/subject/1054917/comments/'
head = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'}
#解析HTML
r = requests.get(url, headers=head)
s = etree.HTML(r.text)
comments = s.xpath('//div[@class="comment"]/p/text()')
#print(str(comments))#在寫代碼的時(shí)候可以將讀取的內(nèi)容打印一下
#保存數(shù)據(jù)open函數(shù)
with open('D:/PythonWorkSpace/TestData/pinglun.txt','w',encoding='utf-8') as f:#使用with open()新建對(duì)象f
for i in comments:
print(i)
f.write(i+'\n')#寫入數(shù)據(jù),文件保存在上面指定的目錄,加\n為了換行更方便閱讀這里指的注意的是: open函數(shù)的打開模式

pandas包保存
使用pandas保存數(shù)據(jù)到CSV和Excel:
#導(dǎo)入包import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,4))#創(chuàng)建隨機(jī)值
#print(df.head(2))#查看數(shù)據(jù)框的頭部數(shù)據(jù),默認(rèn)不寫為前5行,小于5行時(shí)全部顯示;也可以自定義查看幾行
print(df.tail())##查看數(shù)據(jù)框的尾部數(shù)據(jù),默認(rèn)不寫為倒數(shù)5行,小于5行時(shí)全部顯示;也可以自定義查看倒數(shù)幾行
df.to_csv('D:/PythonWorkSpace/TestData/PandasNumpy.csv')#存儲(chǔ)到CSV中
#df.to_excel('D:/PythonWorkSpace/TestData/PandasNumpy.xlsx')#存儲(chǔ)到Excel中(需要提前導(dǎo)入庫(kù) pip install openpyxl)實(shí)例中保存豆瓣讀書的短評(píng)代碼如下:
import requests
from lxml import etree
#發(fā)送Request請(qǐng)求
url = 'https://book.douban.com/subject/1054917/comments/'
head = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'}
#解析HTML
r = requests.get(url, headers=head)
s = etree.HTML(r.text)
comments = s.xpath('//div[@class="comment"]/p/text()')
#print(str(comments))#在寫代碼的時(shí)候可以將讀取的內(nèi)容打印一下
'''
#保存數(shù)據(jù)open函數(shù)
with open('D:/PythonWorkSpace/TestData/pinglun.txt','w',encoding='utf-8') as f:#使用with open()新建對(duì)象f
for i in comments:
print(i)
f.write(i+'\n')#寫入數(shù)據(jù),文件保存在上面指定的目錄,加\n為了換行更方便閱讀
'''
#保存數(shù)據(jù)pandas函數(shù) 到CSV 和Excel
import pandas as pd
df = pd.DataFrame(comments)
#print(df.head())#head()默認(rèn)為前5行
df.to_csv('D:/PythonWorkSpace/TestData/PandasNumpyCSV.csv')
#df.to_excel('D:/PythonWorkSpace/TestData/PandasNumpyEx.xlsx')
看完了這篇文章,相信你對(duì)保存python程序所生產(chǎn)數(shù)據(jù)的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站名稱:保存python程序所生產(chǎn)數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article34/dpgdpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、虛擬主機(jī)、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站設(shè)計(jì)、響應(yīng)式網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容