最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python操作mysql、excel、pdf的示例

 更新時(shí)間:2021年03月29日 14:42:57   作者:JKYEC | Jake  
這篇文章主要介紹了python操作mysql、excel、pdf的示例,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下

一、學(xué)習(xí)如何定義一個(gè)對(duì)象

代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 1. 定義Person類
class Person:

  def __init__(self, name, age):
    self.name = name
    self.age = age

  def watch_tv(self):
    print(f'{self.name} 看電視')


# 2. 定義loop函數(shù)
# 打印 1-max 中的奇數(shù)
def test_person():
  person = Person('Jake', 20)
  print(f'打印person的地址:', person)
  print(f'person.name:{person.name}')
  print(f'person.age:{person.age}')
  person.watch_tv()

  person = Person('Koko', 18)
  print(f'打印person的地址:', person)
  print(f'person.name:{person.name}')
  print(f'person.age:{person.age}')
  person.watch_tv()


# 3. 執(zhí)行calculate方法
# 計(jì)算 當(dāng)前值小于1,當(dāng)前值:0
# 計(jì)算 1 >= 1: True
# 計(jì)算 2 >= 1: True
# 計(jì)算 10 >= 1: True
test_person()

執(zhí)行結(jié)果:

二、學(xué)習(xí)如何連接MySQL并查詢

代碼塊:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# pip3 install pymysql

import pymysql

from getpass import getpass

# from mysql.connector import connect, Error
#
host = 'xxxxxxx'
port = 3306
username = 'db_account_member'
password = 'db_account_password'
database = 'some_database'


def connect_db():
  return pymysql.connect(host=host,
              port=port,
              user=username,
              password=password,
              database=database,
              charset='utf8')


def print_error(e):
  print(f'錯(cuò)誤類型:{type(e)}')
  print(f'錯(cuò)誤內(nèi)容:{e}')


def close_gracefully(cursor, conn):
  if cursor:
    cursor.close()
  if conn:
    conn.close()


# 查詢數(shù)據(jù)庫,可以寫任意查詢語句
def query(sql):
  try:
    conn = connect_db() # 創(chuàng)建連接
    cursor = conn.cursor() # 建立游標(biāo)
    cursor.execute(sql) # 執(zhí)行sql語句
    return cursor.fetchall()
  except pymysql.Error as e:
    print_error(e)
  finally:
    close_gracefully(cursor, conn)



query_sql = 'select * from category where id = 1'
rows = query(query_sql)
print('category表中的數(shù)據(jù)如下:')
print(rows)

執(zhí)行結(jié)果:

三、學(xué)習(xí)如何讀寫csv

代碼:

# -*- coding: UTF-8 -*-

# 1. 導(dǎo)入csv庫
import csv

file_name = '../resources/test.csv'

# 2. 定義headers和rows
headers = ['index', 'name', 'sex', 'height', 'year']

rows = [
  [1, 'Jake', 'male', 177, 20],
  [2, 'Koko', 'female', 165, 18],
  [3, 'Mother', 'female', 163, 45],
  [4, 'Father', 'male', 172, 48]
]


# 3. 定義write_csv函數(shù)
# 寫入csv
def write_csv():
  print(f'文件[{file_name}]準(zhǔn)備寫入')
  with open(f'{file_name}', 'w')as f:
    f_csv = csv.writer(f)
    f_csv.writerow(headers)
    f_csv.writerows(rows)
    print(f'文件[{file_name}]寫入完畢')


# 讀取csv
def read_csv():
  print(f'文件[{file_name}]準(zhǔn)備讀取')
  with open(f'{file_name}')as f:
    f_csv = csv.reader(f)
    for row in f_csv:
      print(row)
  print(f'文件[{file_name}]讀取完畢')


# 4. 執(zhí)行write_csv函數(shù)
write_csv()
print('------')
read_csv()


執(zhí)行結(jié)果:

四、讀取xlsx

代碼:

# -*- coding: UTF-8 -*-

# 導(dǎo)引
# 安裝相關(guān)依賴
# pip3 install xlrd

# 引入xlrd去支持讀取xls相關(guān)的文件
import xlrd

# 定義文件名
file_name = '../resources/sku.xls'

# 1. 讀取xls文件
# 預(yù)計(jì)輸出
# sku.xls該文檔有 3 個(gè)tab頁
sku_file = xlrd.open_workbook(file_name)
print("{0}該文檔有 {1} 個(gè)tab頁".format(file_name, sku_file.nsheets))
print("每個(gè)tab頁,頁名分別為: {0}".format(sku_file.sheet_names()))

# 2. 讀取xls文件第1頁
# 預(yù)計(jì)輸出
# tab頁名:Sheet1,該tab頁共有59行,3列
# A6方格的值:1908165140370878
current_sheet_index = 0 # 下標(biāo)0為第一頁tab
current_sheet = sku_file.sheet_by_index(current_sheet_index)
print("tab頁名:{0},該tab頁共有{1}行,{2}列".format(current_sheet.name, current_sheet.nrows, current_sheet.ncols))
print("A6方格的值:{0}".format(current_sheet.cell_value(rowx=5, colx=0)))

# 3. 打印每頁的數(shù)據(jù),每一行的數(shù)據(jù)為一個(gè)數(shù)組
# 預(yù)計(jì)輸出
# [text:'1908154975415329', text:'鞋面是織物 鞋底是聚氨酯底的哦', text:'鞋底是5厘米 內(nèi)增是3厘米 總高度是8厘米左右哦']
# [text:'1908040228021948', text:'鞋面是飛織 鞋底是聚氨酯底的哦', text:'鞋底高度是3厘米左右哦']
# ...以下省略后續(xù)打印
for rx in range(current_sheet.nrows):
  print(current_sheet.row(rx))

執(zhí)行結(jié)果:

五、讀寫PDF

代碼:

import platform
import pdfkit

# 這里根據(jù)自己的系統(tǒng)修改對(duì)應(yīng)的wkhtmltopdf安裝路徑,修改其中一個(gè)就行了
win_path = 'D:/tools/wkhtmltopdf'
non_win_path = '/usr/local/bin/wkhtmltopdf'


def wkhtmltopdf_path():
  system = platform.system()
  if system == 'Darwin':
    print('蘋果系統(tǒng),可以生成pdf')
    path = non_win_path
  elif system == 'Windows':
    print('Windows系統(tǒng),可以生成pdf')
    path = win_path
  elif system == 'Linux系統(tǒng)':
    print('Linux系統(tǒng),可以生成pdf')
    path = non_win_path
  else:
    print('其他系統(tǒng),暫不支持生成pdf')
    raise Exception('其他系統(tǒng),暫不支持生成pdf')
  return path


def pre_config():
  return pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path())


# 從鏈接地址生成pdf
def generate_pdf_from_url(url, output_file_path):
  config = pre_config()
  pdfkit.from_url(url, output_file_path)


# 從字符串生成pdf
def generate_pdf_from_string(str, output_file_path):
  config = pre_config()
  pdfkit.from_string(str, output_file_path)


generate_pdf_from_url('https://baidu.com', '../temp/baidu_test.pdf')

generate_pdf_from_string('hello', '../temp/hello.pdf')

wkhtmltopdf這個(gè)東西一定要裝,不然無法生成pdf,會(huì)報(bào)IO方面的錯(cuò)誤,小白照做就可以,不需要理解

執(zhí)行結(jié)果

生成的文件長這個(gè)樣子

baidu_test.pdf

hello.pdf

以上就是python操作mysql、excel、pdf的示例的詳細(xì)內(nèi)容,更多關(guān)于python操作mysql、excel、pdf的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python非阻塞式后臺(tái)如何運(yùn)行bat腳本

    python非阻塞式后臺(tái)如何運(yùn)行bat腳本

    這篇文章主要介紹了python非阻塞式后臺(tái)如何運(yùn)行bat腳本問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Pytorch中的學(xué)習(xí)率衰減及其用法詳解

    Pytorch中的學(xué)習(xí)率衰減及其用法詳解

    這篇文章主要介紹了Pytorch中的學(xué)習(xí)率衰減及其用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python 制作詞云的WordCloud參數(shù)用法說明

    Python 制作詞云的WordCloud參數(shù)用法說明

    這篇文章主要介紹了Python 制作詞云的WordCloud參數(shù)用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • calendar在python3時(shí)間中常用函數(shù)舉例詳解

    calendar在python3時(shí)間中常用函數(shù)舉例詳解

    這篇文章主要介紹了calendar在python3時(shí)間中常用函數(shù)的相關(guān)文章,對(duì)此知識(shí)點(diǎn)有興趣的朋友們可以學(xué)習(xí)下。
    2020-11-11
  • Python偽隨機(jī)數(shù)模塊random詳解

    Python偽隨機(jī)數(shù)模塊random詳解

    這篇文章主要為大家詳細(xì)介紹了Python偽隨機(jī)數(shù)模塊random,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • python+ollama自己寫代碼調(diào)用本地deepseek模型

    python+ollama自己寫代碼調(diào)用本地deepseek模型

    本文主要介紹了python+ollama自己寫代碼調(diào)用本地deepseek模型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • Python 模擬員工信息數(shù)據(jù)庫操作的實(shí)例

    Python 模擬員工信息數(shù)據(jù)庫操作的實(shí)例

    下面小編就為大家?guī)硪黄狿ython 模擬員工信息數(shù)據(jù)庫操作的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • Python?selenium下拉選擇框?qū)崙?zhàn)應(yīng)用例子

    Python?selenium下拉選擇框?qū)崙?zhàn)應(yīng)用例子

    Selenium是一個(gè)開源的和便攜式的自動(dòng)化軟件測(cè)試工具,用于測(cè)試Web應(yīng)用程序有能力在不同的瀏覽器和操作系統(tǒng)運(yùn)行,下面這篇文章主要給大家介紹了關(guān)于Python?selenium下拉選擇框?qū)崙?zhàn)應(yīng)用的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Python類的定義繼承調(diào)用比較方法技巧

    Python類的定義繼承調(diào)用比較方法技巧

    這篇文章主要介紹了Python類的定義繼承調(diào)用比較方法技巧,文章首先通過類的約束展開詳情圍繞主題介紹相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • Pycharm debug調(diào)試時(shí)帶參數(shù)過程解析

    Pycharm debug調(diào)試時(shí)帶參數(shù)過程解析

    這篇文章主要介紹了Pycharm debug調(diào)試時(shí)帶參數(shù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論

巴青县| 余姚市| 湄潭县| 满洲里市| 延川县| 高淳县| 周至县| 永和县| 普格县| 崇州市| 介休市| 上杭县| 武鸣县| 乌苏市| 玉龙| 石楼县| 滨海县| 温州市| 宣城市| 彭州市| 江源县| 哈巴河县| 柘城县| 朝阳县| 阜南县| 金乡县| 横峰县| 四平市| 岳池县| 长乐市| 玉门市| 成武县| 赤峰市| 闽清县| 金华市| 宽甸| 徐闻县| 固安县| 岳阳市| 临西县| 万年县|