shelve
shelve是一個簡單的資料儲存方案,他只有一個函數就是open(),這個函數接收一個參數就是檔名,然後回傳一個shelf對象,你可以用他來儲存東西,就可以簡單的把他當作一個字典,當你儲存完畢的時候,就呼叫close函數來關閉
這個有一個潛在的小問題,如下:
[python] view plaincopy
>>> import shelve
>>> s = shelve. open('test.dat')
>>> s['x'] = ['a', 'b', 'c']
>>> s['x'].append('d')
>>> s['x']
['a', 'b', 'c']
儲存的d到哪裡去了呢?其實很簡單,d沒有寫回,你把['a', 'b', 'c']存到了x,當你再次讀取s['x']的時候,s['x']只是一個拷貝,而你沒有將拷貝寫回,所以當你再次讀取s['x']的時候,它又從源中讀取了一個拷貝,所以,你新修改的內容並不會出現在拷貝中,解決的辦法就是,第一個是利用一個緩存的變量,如下所示
[python] view plaincopy
>>> temp = s['x']
>>> temp.append('d')
>>> s['x'] = temp
>>> s['x']
['a', 'b', 'c', 'd']
在python2.4中有了另一個的方法,就是把open方法的writeback參數的值賦為True,這樣的話,你open後所有的內容都將在cache中,當你close的時候,將全部一次性寫到硬碟裡面。如果資料量不是很大的時候,建議這麼做。
下面是一個基於shelve的簡單資料庫的程式碼
[python] view plaincopy
#database.py
import sys, shelve
user for data and store it in the shelf object
"""
pid = raw_input('Enter unique ID number: ')
) person['age'] = raw_input( 'Enter age: ')
person['phone'] = raw_input('Enter phone number: ')
db[pid] = person
Query user for ID and desired field , and fetch the corresponding data from
the shelf object
"""
pid = raw_input('Enter number: ') ') phone) ')
field = field.strip().lower()
print field.capitalize() + ':',
db[pid able commons are: '
print ' store :Stores information about a person'
print 'lookup :Looks up a person from ID number'
print 'quit print:Save s and exave '
def enter_command():
cmd = raw_input ('Enter command (? for help): ')
cmd = cmd.strip().lower()
return cmd
, main(():openomain(com):open
try:
while True:
cmd = enter_command() store_person(database)
elif cmd == '查找':
Lookup_person(資料庫)
print_help()
elif cmd == '退出':
database.close()
if __name__ = = '__main__': main()

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中