python學習day21 模組和包

2022-09-13 23:42:33 字數 2743 閱讀 2694

#

序列化模組

#資料型別轉化成字串的過程

#為了方便儲存和網路傳輸

#三種模式:

#json:

#dumps

#loads

#dump 和檔案有關

#load 不能load多次

#import json

#data =

#json_dic = json.dumps(data,sort_keys=true,indent=4,separators=(',',':,'),ensure_ascii=false)

#print(json_dic)

#pickle:

#方法與json一樣

#dump和load的時候,檔案是以rb,wb

#支援python所有的資料型別

#shelve:

#open方法

#獲取了乙個檔案控制代碼

#操作和字典類似

#把解決一類問題的模組放在同乙個資料夾裡 —— 包

#import os

#os.makedirs('glance/api')

#os.makedirs('glance/cmd')

#os.makedirs('glance/db')

#l = ##

####

##map(lambda f:f.close() ,l)

#import glance.api.policy as policy

#policy.get()##

from dir.glance.api import policy

#policy.get()

#import sys

#sys.path.insert(0,'c:\\users\\administrator\\pycharmprojects\\s9\\day21\\dir')

## print(sys.path)

#from glance.api import policy

#policy.get()

#from dir import glance

#glance.db.models.register_models('mysql')

#glance.api.policy.get()

#使用絕對路徑 不管在包內部還是外部 匯入了就能用

#不能挪動,但是直觀

#from dir import glance

#glance.api.policy.get()

#相對路徑

#可以隨意移動包 只要能找到包的位置,就可以使用包裡的模組

#包裡的模組如果想使用其它模組的內容只能使用相對路徑,使用了相對路徑就不能在包內直接執行了

#1/0

#name

#2+'3'

#[3]

#{}['k']

#try:

#print('1111')

## 1/0

#print('2222')

## name

## 2+'3'

## [3]

## {}['k']

#ret = int(input('number >>>'))

#print(ret*'*')

#except valueerror:

#print('輸入的資料型別有誤')

#except exception:

#print('你錯了,老鐵')

#else:

#print('沒有異常的時候執行else中的**')

#print('**********=')

#def func():

#try:

#f = open('file','w')

#''''''

#return true

#except:

#return false

#finally:

#print('執行finally了')

#f.close()##

print(func())

#程式一旦發生錯誤,就從錯誤的位置停下來了,不在繼續執行後面的內容

#使用try和except就能處理異常

#try是我們需要處理的**

#except 後面跟乙個錯誤型別 當**發生錯誤且錯誤型別符合的時候 就會執行except中的**

#except支援多分支

#有沒有乙個能處理所有錯誤的型別 : exception

#有了萬能的處理機制仍然需要把能**到的問題單獨處理

#單獨處理的所有內容都應該寫在萬能異常之前

#else : 沒有異常的時候執行else中的**

#finally : 不管**是否異常,都會執行

#finally和return相遇的時候 依然會執行

#函式裡做異常處理用,不管是否異常去做一些收尾工作

#try:

#main()

#except exception:

#pass

#try:

#print('1111')

## 1/0

#print('2222')

## name

## 2+'3'

## [3]

## {}['k']

#ret = int(input('number >>>'))

#print(ret*'*')

#except exception as error:

#print('你錯了,老鐵',error)

day21 學習總結

string s new string 建立乙個空的字串序列 string s new string hello 建立乙個內容為 hello 的字串 string s hello 建立乙個內容為 hello 的字串 string s new string char chars 通過字元陣列建立乙個字...

day21 學習筆記

乙個python檔案有兩種用途 1.被當做程式執行 2.被當做模組匯入 二者的區別 ps name main import匯入模組在使用時必須加上字首 模組.優點 肯定不會與當前空間中名字衝突 缺點 顯得麻煩 from模組名import函式名 函式名是在當前位置的全域性變數,但是指向的記憶體位址是模...

python學習day21 包 異常處理

包 包的本質就是乙個包含 init py檔案的目錄 在python3中,即使包下沒有 init py檔案,import 包仍然不會報錯,而在python2中,包下一定要有該檔案,否則import 包報錯 凡是在匯入時帶點的,點的左邊都必須是乙個包 如果不想在匯入模組時打很多點,可以新增路徑 檢視可匯...