random os sys 序列化模組

2022-02-15 05:51:42 字數 4792 閱讀 5919

print(random.randint(1,6))  # 隨機取乙個你提供的整數範圍內的數字  包含首尾

print(random.random()) # 隨機取0-1之間小數

print(random.choice([1,2,3,4,5,6])) # 搖號 隨機從列表中取乙個元素

"""

大寫字母 小寫字母 數字

5位數的隨機驗證碼

封裝成乙個函式,使用者想生成幾位就生成幾位

"""def get_code(n):

code = ''

for i in range(n):

# 先生成隨機的大寫字母 小寫字母 數字

upper_str = chr(random.randint(65,90))

lower_str = chr(random.randint(97,122))

random_int = str(random.randint(0,9))

# 從上面三個中隨機選擇乙個作為隨機驗證碼的某一位

code += random.choice([upper_str,lower_str,random_int])

return code

res = get_code(5)

print(res)

import random

#隨機小數

>>> random.random() # 大於0且小於1之間的小數

0.7664338663654585

>>> random.uniform(1,3) #大於1小於3的小數

1.6270147180533838

#隨機整數

>>> random.randint(1,5) # 大於等於1且小於等於5之間的整數

>>> random.randrange(1,10,2) # 大於等於1且小於10之間的奇數

#隨機選擇乙個返回

>>> random.choice([1,'23',[4,5]]) # #1或者23或者[4,5]

#隨機選擇多個返回,返回的個數為函式的第二個引數

>>> random.sample([1,'23',[4,5]],2) # #列表元素任意2個組合

[[4, 5], '23']

#打亂列表順序

>>> item=[1,3,5,7,9]

>>> random.shuffle(item) # 打亂次序

>>> item

[5, 1, 3, 7, 9]

>>> random.shuffle(item)

>>> item

[5, 9, 7, 1, 3]

import os 

base_dir = os.path.dirname(__file__)

返回path的目錄 這裡的__file__是絕對路徑

os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第乙個絕對路徑之前的引數將被忽略

base_dir = os.path.dirname(__file__)

movie_dir = os.path.join(base_dir,'老師們的作品')

movie_list = os.listdir(movie_dir)

print(movie_list )

os.listdir('dirname') 列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印

while true:

for i,j in enumerate(movie_list,1):

print(i,j)

choice = input('你想看誰的啊(今日熱搜:tank老師)>>>:').strip()

if choice.isdigit(): # 判斷使用者輸入的是否是純數字

choice = int(choice) # 傳成int型別

if choice in range(1,len(movie_list)+1): # 判斷是否在列表元素個數範圍內

# 獲取使用者想要看的檔名

target_file = movie_list[choice-1]

# 拼接檔案絕對路徑

target_path = os.path.join(movie_dir,target_file)

with open(target_path,'r',encoding='utf-8') as f:

print(f.read())

os.mkdir('tank老師精選') # 自動建立資料夾

print(os.path.exists(r'd:\python專案\day16\rion老師精選')) # 判斷檔案是否存在

print(os.path.exists(r'd:\python專案\day16\老師們的作品\tank老師.txt')) # 判斷檔案是否存在

print(os.path.isfile(r'd:\python專案\day16\tank老師精選')) # 只能判斷檔案 不能判斷資料夾

print(os.path.isfile(r'd:\python專案\day16\老師們的作品\tank老師.txt')) # 只能判斷檔案 不能判斷資料夾

os.rmdir(r'd:\python專案\day16\老師們的作品') # 只能刪空資料夾

print(os.getcwd())

print(os.chdir(r'd:\python專案\day16\老師們的作品')) # 切換當前所在的目錄

print(os.getcwd())

獲取檔案大小

print(os.path.getsize(r'd:\python專案\day16\老師們的作品\tank老師.txt')) # 位元組大小

with open(r'd:\python專案\day16\老師們的作品\tank老師.txt',encoding='utf-8') as f:

print(len(f.read()))

os.remove() 刪除乙個檔案

os.rename("oldname","newname") 重新命名檔案/目錄

os.path.exists(path) 如果path存在,返回true;如果path不存在,返回false

os.path.isabs(path) 如果path是絕對路徑,返回true

os.path.isfile(path) 如果path是乙個存在的檔案,返回true。否則返回false

os.path.isdir(path) 如果path是乙個存在的目錄,則返回true。否則返回false

os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第乙個絕對路徑之前的引數將被忽略

os.path.getatime(path) 返回path所指向的檔案或者目錄的最後訪問時間

os.path.getmtime(path) 返回path所指向的檔案或者目錄的最後修改時間

os.path.getsize(path) 返回path的大小

json模組提供了四個功能:dumps、dump、loads、load

import json

dic =

str_dic = json.dumps(dic) #序列化:將乙個字典轉換成乙個字串

print(type(str_dic),str_dic) #

#注意,json轉換完的字串型別的字典中的字串是由""表示的

dic2 = json.loads(str_dic) #反序列化:將乙個字串格式的字典轉換成乙個字典

#注意,要用json的loads功能處理的字串型別的字典中的字串必須由""表示

print(type(dic2),dic2) #

list_dic = [1,['a','b','c'],3,]

str_dic = json.dumps(list_dic) #也可以處理巢狀的資料型別

print(type(str_dic),str_dic) #[1, ["a", "b", "c"], 3, ]

list_dic2 = json.loads(str_dic)

print(type(list_dic2),list_dic2) #[1, ['a', 'b', 'c'], 3, ]

import json

data =

json_dic2 = json.dumps(data,sort_keys=true,indent=2,separators=(',',':'),ensure_ascii=false)

print(json_dic2)

pickle

import pickle

dic =

str_dic = pickle.dumps(dic)

print(str_dic) #一串二進位制內容

dic2 = pickle.loads(str_dic)

print(dic2) #字典

import time

struct_time = time.localtime(1000000000)

print(struct_time)

f = open('pickle_file','wb')

pickle.dump(struct_time,f)

f.close()

f = open('pickle_file','rb')

struct_time2 = pickle.load(f)

print(struct_time2.tm_year

序列化 json pickle shelve模組

序列化 把乙個物件,從記憶體中變成乙個可儲存,可傳輸的過程。json 不能序列化函式和類。json模組可以與其它程式進行介面對接。pickle 是python專有的乙個序列化模組,可以序列化函式,但不能與其它語言進行介面。shelve模組更靈活,只有乙個open方法。import json dic ...

序列化(序列化)

原書上翻譯為序列化,msdn翻譯為序列化 作用 當需要儲存,或者網路傳輸 remoting時,資料 物件或值 需要序列化 類似於打包傳輸檔案。system.serializableattribute 序列化是指儲存和獲取磁碟檔案 記憶體或其他地方中的物件。在序列化時,所有的例項資料都儲存到儲存介質上...

序列化(模型序列化 序列化巢狀)

from rest framework import serializers from meituan.models import merchant,class merchantserializer serializers.modelserializer class meta model merch...