内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

python怎樣讀寫csv文件-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)python怎樣讀寫csv文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),西雙版納企業(yè)網(wǎng)站建設(shè),西雙版納品牌網(wǎng)站建設(shè),網(wǎng)站定制,西雙版納網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,西雙版納網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

1.爬取豆瓣top250書籍

import requests
import json
import csv
from bs4 import BeautifulSoup
books = []
def book_name(url):
 res = requests.get(url)
 html = res.text
 soup = BeautifulSoup(html, 'html.parser')
 items = soup.find(class_="grid-16-8 clearfix").find(class_="indent").find_all('table')
 for i in items:
  book = []
  title = i.find(class_="pl2").find('a')
  book.append('《' + title.text.replace(' ', '').replace('\n', '') + '》')
  star = i.find(class_="star clearfix").find(class_="rating_nums")
  book.append(star.text + '分')
  try:
   brief = i.find(class_="quote").find(class_="inq")
  except AttributeError:
   book.append('”暫無簡介“')
  else:
   book.append(brief.text)
  link = i.find(class_="pl2").find('a')['href']
  book.append(link)
  global books
  books.append(book)
  print(book)
 try:
  next = soup.find(class_="paginator").find(class_="next").find('a')['href']
 # 翻到最后一頁
 except TypeError:
  return 0
 else:
  return next
next = 'https://book.douban.com/top250?start=0&filter='
count = 0
while next != 0:
 count += 1
 next = book_name(next)
 print('-----------以上是第' + str(count) + '頁的內(nèi)容-----------')
csv_file = open('D:/top250_books.csv', 'w', newline='', encoding='utf-8')
w = csv.writer(csv_file)
w.writerow(['書名', '評分', '簡介', '鏈接'])
for b in books:
 w.writerow(b)

結(jié)果

python怎樣讀寫csv文件

2.把評分為9.0的書籍保存到book_out.csv文件中

'''
1.爬取豆瓣評分排行前250本書,保存為top250.csv
2.讀取top250.csv文件,把評分為9.0以上的書籍保存到另外一個(gè)csv文件中
'''
import csv
#打開的時(shí)候必須用encoding='utf-8',否則報(bào)錯(cuò)
with open('top250.csv', encoding='utf-8') as rf:
 reader = csv.reader(rf)
 #讀取頭部
 headers = next(reader)
 with open('books_out.csv', 'w', encoding='utf-8') as wf:
  writer = csv.writer(wf)
  #把頭部信息寫進(jìn)去
  writer.writerow(headers)
  for book in reader:
   #獲取評分
   score = book[1]
   #把評分大于9.0的過濾出來
   if score and float(score) >= 9.0:
    writer.writerow(book)

感謝各位的閱讀!關(guān)于“python怎樣讀寫csv文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章名稱:python怎樣讀寫csv文件-創(chuàng)新互聯(lián)
當(dāng)前URL:http://www.rwnh.cn/article46/cepphg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、做網(wǎng)站、網(wǎng)站維護(hù)、網(wǎng)站內(nèi)鏈品牌網(wǎng)站制作、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
天峻县| 朝阳县| 徐州市| 中卫市| 巴林左旗| 广德县| 威远县| 尼玛县| 张北县| 卢湾区| 茶陵县| 涿鹿县| 嘉荫县| 平利县| 河北省| 太和县| 明溪县| 辰溪县| 大安市| 开平市| 安龙县| 天全县| 罗甸县| 浏阳市| 钟山县| 富裕县| 凌源市| 旺苍县| 宁都县| 射洪县| 阿克苏市| 崇信县| 山东省| 时尚| 始兴县| 鱼台县| 富民县| 商南县| 河北区| 和顺县| 乐昌市|