国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python操作mysql、excel、pdf的示例

瀏覽:87日期:2022-06-23 18:23:12
一、學(xué)習(xí)如何定義一個(gè)對(duì)象

代碼:

#!/usr/bin/python# -*- coding: UTF-8 -*-# 1. 定義Person類(lèi)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: Truetest_person()

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

python操作mysql、excel、pdf的示例

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

代碼塊:

#!/usr/bin/python# -*- coding: UTF-8 -*-# pip3 install pymysqlimport pymysqlfrom getpass import getpass# from mysql.connector import connect, Error#host = ’xxxxxxx’port = 3306username = ’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ò)誤類(lèi)型:{type(e)}’) print(f’錯(cuò)誤內(nèi)容:{e}’)def close_gracefully(cursor, conn): if cursor: cursor.close() if conn: conn.close()# 查詢(xún)數(shù)據(jù)庫(kù),可以寫(xiě)任意查詢(xún)語(yǔ)句def query(sql): try: conn = connect_db() # 創(chuàng)建連接 cursor = conn.cursor() # 建立游標(biāo) cursor.execute(sql) # 執(zhí)行sql語(yǔ)句 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é)果:

python操作mysql、excel、pdf的示例

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

代碼:

# -*- coding: UTF-8 -*-# 1. 導(dǎo)入csv庫(kù)import csvfile_name = ’../resources/test.csv’# 2. 定義headers和rowsheaders = [’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ù)# 寫(xiě)入csvdef write_csv(): print(f’文件[{file_name}]準(zhǔn)備寫(xiě)入’) 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}]寫(xiě)入完畢’)# 讀取csvdef 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é)果:

python操作mysql、excel、pdf的示例

四、讀取xlsx

代碼:

# -*- coding: UTF-8 -*-# 導(dǎo)引# 安裝相關(guān)依賴(lài)# pip3 install xlrd# 引入xlrd去支持讀取xls相關(guān)的文件import xlrd# 定義文件名file_name = ’../resources/sku.xls’# 1. 讀取xls文件# 預(yù)計(jì)輸出# sku.xls該文檔有 3 個(gè)tab頁(yè)sku_file = xlrd.open_workbook(file_name)print('{0}該文檔有 {1} 個(gè)tab頁(yè)'.format(file_name, sku_file.nsheets))print('每個(gè)tab頁(yè),頁(yè)名分別為: {0}'.format(sku_file.sheet_names()))# 2. 讀取xls文件第1頁(yè)# 預(yù)計(jì)輸出# tab頁(yè)名:Sheet1,該tab頁(yè)共有59行,3列# A6方格的值:1908165140370878current_sheet_index = 0 # 下標(biāo)0為第一頁(yè)tabcurrent_sheet = sku_file.sheet_by_index(current_sheet_index)print('tab頁(yè)名:{0},該tab頁(yè)共有{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. 打印每頁(yè)的數(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é)果:

python操作mysql、excel、pdf的示例

五、讀寫(xiě)PDF

代碼:

import platformimport 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(’蘋(píng)果系統(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 pathdef pre_config(): return pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path())# 從鏈接地址生成pdfdef generate_pdf_from_url(url, output_file_path): config = pre_config() pdfkit.from_url(url, output_file_path)# 從字符串生成pdfdef 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è)東西一定要裝,不然無(wú)法生成pdf,會(huì)報(bào)IO方面的錯(cuò)誤,小白照做就可以,不需要理解

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

python操作mysql、excel、pdf的示例

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

python操作mysql、excel、pdf的示例

baidu_test.pdf

python操作mysql、excel、pdf的示例

hello.pdf

python操作mysql、excel、pdf的示例

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

標(biāo)簽: python
相關(guān)文章:
主站蜘蛛池模板: 国产成人久久精品区一区二区 | 最新国产美女肝交视频播放 | 国产欧美精品一区二区三区四区 | 九九综合 | 亚洲国产毛片aaaaa无费看 | 亚洲国产网址 | 日本一级毛片中文字幕 | 免费人成网站在线播放 | aaa在线| 欧美另类激情 | 综合自拍亚洲综合图区美腿丝袜 | 成年人在线观看视频免费 | 欧美性视频一区二区三区 | 免费一级欧美片在线观免看 | 正在播放国产一区 | 亚洲国产成人久久综合野外 | 亚洲三级视频在线观看 | 国产免费自拍 | 久久久久久久亚洲精品一区 | 高清一区二区三区四区五区 | 失禁h啪肉尿出来高h男男 | 欧美videofree性欧美另类 | 免费一级特黄欧美大片久久网 | 曰本美女高清在线观看免费 | 六月伊人| 91欧美一区二区三区综合在线 | 国产第一页久久亚洲欧美国产 | 九九九热视频 | baoyu121永久免费网站 | 久草视频精品 | 亚洲香蕉久久一区二区三区四区 | 国产亚洲精品一区二区三区 | 一级片一区 | 国内一区| 一区二区欧美视频 | 免费午夜扒丝袜www在线看 | 日韩欧美毛片免费看播放 | 亚欧成人| 国产一级特黄特色aa毛片 | 免费一区二区三区在线视频 | 日本韩国一区 |