<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          分享幾段祖?zhèn)鞯腜ython代碼,拿來直接使用!

          共 15941字,需瀏覽 32分鐘

           ·

          2022-03-04 14:11

          今天分享幾段工作生活中常用的代碼,都是最為基礎的功能和操作,而且大多還都是出現(xiàn)頻率比較高的,很多都是可以拿來直接使用或者簡單修改就可以放到自己的項目當中

          日期生成

          很多時候我們需要批量生成日期,方法有很多,這里分享兩段代碼

          獲取過去 N 天的日期

          import datetime

          def get_nday_list(n):
              before_n_days = []
              for i in range(1, n + 1)[::-1]:
                  before_n_days.append(str(datetime.date.today() - datetime.timedelta(days=i)))
              return before_n_days

          a = get_nday_list(30)
          print(a)

          Output:

          ['2021-12-23', '2021-12-24', '2021-12-25', '2021-12-26', '2021-12-27', '2021-12-28', '2021-12-29', '2021-12-30', '2021-12-31', '2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12', '2022-01-13', '2022-01-14', '2022-01-15', '2022-01-16', '2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20', '2022-01-21']

          生成一段時間區(qū)間內(nèi)的日期

          import datetime

          def create_assist_date(datestart = None,dateend = None):
              # 創(chuàng)建日期輔助表

              if datestart is None:
                  datestart = '2016-01-01'
              if dateend is None:
                  dateend = datetime.datetime.now().strftime('%Y-%m-%d')

              # 轉為日期格式
              datestart=datetime.datetime.strptime(datestart,'%Y-%m-%d')
              dateend=datetime.datetime.strptime(dateend,'%Y-%m-%d')
              date_list = []
              date_list.append(datestart.strftime('%Y-%m-%d'))
              while datestart<dateend:
                  # 日期疊加一天
                  datestart+=datetime.timedelta(days=+1)
                  # 日期轉字符串存入列表
                  date_list.append(datestart.strftime('%Y-%m-%d'))
              return date_list

          d_list = create_assist_date(datestart='2021-12-27', dateend='2021-12-30')
          d_list

          Output:

          ['2021-12-27', '2021-12-28', '2021-12-29', '2021-12-30']

          保存數(shù)據(jù)到CSV

          保存數(shù)據(jù)到 CSV 是太常見的操作了,分享一段我個人比較喜歡的寫法

          def save_data(data, date):
              if not os.path.exists(r'2021_data_%s.csv' % date):
                  with open("2021_data_%s.csv" % date, "a+", encoding='utf-8'as f:
                      f.write("標題,熱度,時間,url\n")
                      for i in data:
                          title = i["title"]
                          extra = i["extra"]
                          time = i['time']
                          url = i["url"]
                          row = '{},{},{},{}'.format(title,extra,time,url)
                          f.write(row)
                          f.write('\n')
              else:
                  with open("2021_data_%s.csv" % date, "a+", encoding='utf-8'as f:
                      for i in data:
                          title = i["title"]
                          extra = i["extra"]
                          time = i['time']
                          url = i["url"]
                          row = '{},{},{},{}'.format(title,extra,time,url)
                          f.write(row)
                          f.write('\n')

          帶背景顏色的 Pyecharts

          Pyecharts 作為 Echarts 的優(yōu)秀 Python 實現(xiàn),受到眾多開發(fā)者的青睞,用 Pyecharts 作圖時,使用一個舒服的背景也會給我們的圖表增色不少

          以餅圖為例,通過添加 JavaScript 代碼來改變背景顏色

          def pie_rosetype(data) -> Pie:
              background_color_js = (
              "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
              "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
          )
              c = (
                  Pie(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js)))
                  .add(
                      "",
                      data,
                      radius=["30%""75%"],
                      center=["45%""50%"],
                      rosetype="radius",
                      label_opts=opts.LabelOpts(formatter="{b}: {c}"),
                  )
                  .set_global_opts(title_opts=opts.TitleOpts(title=""),
                                  )
              )
              return c

          requests 庫調用

          據(jù)統(tǒng)計,requests 庫是 Python 家族里被引用的最多的第三方庫,足見其江湖地位之高大!

          發(fā)送 GET 請求

          import requests


          headers = {
              'user-agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
            'cookie''some_cookie'
          }
          response = requests.request("GET", url, headers=headers)

          發(fā)送 POST 請求

          import requests


          payload={}
          files=[]
          headers = {
              'user-agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
            'cookie''some_cookie'
          }
          response = requests.request("POST", url, headers=headers, data=payload, files=files)

          根據(jù)某些條件循環(huán)請求,比如根據(jù)生成的日期

          def get_data(mydate):
              date_list = create_assist_date(mydate)
              url = "https://test.test"
              files=[]
              headers = {
                  'user-agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
                  'cookie'''
                  }
              for d in date_list:
                  payload={'p''10',
                  'day': d,
                  'nodeid''1',
                  't''itemsbydate',
                  'c''node'}
                  for i in range(1100):
                      payload['p'] = str(i)
                      print("get data of %s in page %s" % (d, str(i)))
                      response = requests.request("POST", url, headers=headers, data=payload, files=files)
                      items = response.json()['data']['items']
                      if items:
                          save_data(items, d)
                      else:
                          break

          Python 操作各種數(shù)據(jù)庫

          操作 Redis

          連接 Redis

          import redis


          def redis_conn_pool():
              pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
              rd = redis.Redis(connection_pool=pool)
              return rd

          寫入 Redis

          from redis_conn import redis_conn_pool


          rd = redis_conn_pool()
          rd.set('test_data''mytest')

          操作 MongoDB

          連接 MongoDB

          from pymongo import MongoClient


          conn = MongoClient("mongodb://%s:%s@ipaddress:49974/mydb" % ('username''password'))
          db = conn.mydb
          mongo_collection = db.mydata

          批量插入數(shù)據(jù)

          res = requests.get(url, params=query).json()
          commentList = res['data']['commentList']
          mongo_collection.insert_many(commentList)

          操作 MySQL

          連接 MySQL

          import MySQLdb

          # 打開數(shù)據(jù)庫連接
          db = MySQLdb.connect("localhost""testuser""test123""TESTDB", charset='utf8' )

          # 使用cursor()方法獲取操作游標 
          cursor = db.cursor()

          執(zhí)行 SQL 語句

          # 使用 execute 方法執(zhí)行 SQL 語句
          cursor.execute("SELECT VERSION()")

          # 使用 fetchone() 方法獲取一條數(shù)據(jù)
          data = cursor.fetchone()

          print "Database version : %s " % data

          # 關閉數(shù)據(jù)庫連接
          db.close()

          Output:

          Database version : 5.0.45

          本地文件整理

          整理文件涉及需求的比較多,這里分享的是將本地多個 CSV 文件整合成一個文件

          import pandas as pd
          import os


          df_list = []
          for i in os.listdir():
              if "csv" in i:
                  day = i.split('.')[0].split('_')[-1]
                  df = pd.read_csv(i)
                  df['day'] = day
                  df_list.append(df)
          df = pd.concat(df_list, axis=0)
          df.to_csv("total.txt", index=0)

          多線程代碼

          多線程也有很多實現(xiàn)方式,我們選擇自己最為熟悉順手的方式即可

          import threading
          import time

          exitFlag = 0

          class myThread (threading.Thread):
              def __init__(self, threadID, name, delay):
                  threading.Thread.__init__(self)
                  self.threadID = threadID
                  self.name = name
                  self.delay = delay
              def run(self):
                  print ("開始線程:" + self.name)
                  print_time(self.name, self.delay, 5)
                  print ("退出線程:" + self.name)

          def print_time(threadName, delay, counter):
              while counter:
                  if exitFlag:
                      threadName.exit()
                  time.sleep(delay)
                  print ("%s: %s" % (threadName, time.ctime(time.time())))
                  counter -= 1

          # 創(chuàng)建新線程
          thread1 = myThread(1"Thread-1"1)
          thread2 = myThread(2"Thread-2"2)

          # 開啟新線程
          thread1.start()
          thread2.start()
          thread1.join()
          thread2.join()
          print ("退出主線程")

          異步編程代碼

          異步爬取網(wǎng)站

          import asyncio
          import aiohttp
          import aiofiles

          async def get_html(session, url):
              try:
                  async with session.get(url=url, timeout=8as resp:
                      if not resp.status // 100 == 2:
                          print(resp.status)
                          print("爬取", url, "出現(xiàn)錯誤")
                      else:
                          resp.encoding = 'utf-8'
                          text = await resp.text()
                          return text
              except Exception as e:
                  print("出現(xiàn)錯誤", e)
                  await get_html(session, url)

          使用異步請求之后,對應的文件保存也需要使用異步,即是一處異步,處處異步

          async def download(title_list, content_list):
              async with aiofiles.open('{}.txt'.format(title_list[0]), 'a',
                                       encoding='utf-8'as f:
                  await f.write('{}'.format(str(content_list)))


          以上都是平時用的最多的代碼片段,希望對你有所幫助

          好了,這就是今天分享的全部內(nèi)容,喜歡就點個贊+在看吧~

          有興趣的同學可以火速加入我們的星球
          3周零基礎入門提供10節(jié)課程
          全年12節(jié)趣味實戰(zhàn)項目含源碼,
          每月獎勵優(yōu)秀的Top3同學送書
          專業(yè)的答疑群,大廠的老師保姆式的教學

          如果不滿意,三天內(nèi)隨意退款!一年88,現(xiàn)在優(yōu)惠16元


          掃碼加入,3周零基礎入門




          推薦閱讀:

          入門: 最全的零基礎學Python的問題  | 零基礎學了8個月的Python  | 實戰(zhàn)項目 |學Python就是這條捷徑


          干貨:爬取豆瓣短評,電影《后來的我們》 | 38年NBA最佳球員分析 |   從萬眾期待到口碑撲街!唐探3令人失望  | 笑看新倚天屠龍記 | 燈謎答題王 |用Python做個海量小姐姐素描圖 |碟中諜這么火,我用機器學習做個迷你推薦系統(tǒng)電影


          趣味:彈球游戲  | 九宮格  | 漂亮的花 | 兩百行Python《天天酷跑》游戲!


          AI: 會做詩的機器人 | 給圖片上色 | 預測收入 | 碟中諜這么火,我用機器學習做個迷你推薦系統(tǒng)電影


          小工具: Pdf轉Word,輕松搞定表格和水印! | 一鍵把html網(wǎng)頁保存為pdf!|  再見PDF提取收費! | 用90行代碼打造最強PDF轉換器,word、PPT、excel、markdown、html一鍵轉換 | 制作一款釘釘?shù)蛢r機票提示器! |60行代碼做了一個語音壁紙切換器天天看小姐姐!


          年度爆款文案

          瀏覽 36
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  中文字幕五月天 | 一本色道久久综合无码人妻四虎 | 色色青青资源网 | 国产精品男女 | 免费小黄片视频 |