Python教程:sys模塊中maxsize()的方法

在Python中,sys模塊有一個名爲maxsize()的方法。這個方法返回一個變量Py_ssize_t可以容納的最大值。

Py_ssize_t是一個整數,它給出了變量可以取的最大值。大小因操作系統的位而異。

32位的大小爲(2 power 31)-1,64位的大小爲(2 power 63)-1。

sys.maxsize 方法

sys.maxsize()

返回:此方法根據平臺類型返回最大大小值Py_ssize_t。

代碼1:使用 sys.maxsize() 方法

要實現方法sys.maxsize()並檢查最大大小值,我們可以導入sys模塊並使用方法maxsize()。根據平臺架構類型,sys.maxsize()方法在控制檯上返回其最大值大小。

下面是32位和64位操作系統的實現,並運行相同的sys.maxsize()方法。

32-Bit平臺

# import the sys module to use the maxsize() method
import sys
# returns the maximum size
size = sys.maxsize
print("The maximum size of a 32-bit platform is:" , size)

#輸出:

The maximum size of a 32-bit platform is: 2147483647

64-Bit平臺

import sys
# returns the maximum size
size = sys.maxsize
print("The maximum size of a 32-bit platform is:" , size)

#輸出:

The maximum size of a 64-bit platform is: 9223372036854775807

代碼2:檢查列表的最大大小 sys.maxsize() 方法

爲了檢查我們系統的最大大小,我們可以使用range()方法來傳遞列表的最大大小,然後檢查它的長度。類似地,在第二個例子中,我們超過了最大大小,Python解釋器捕獲了異常並返回int too large to convert to C ssize_t錯誤。

在下面的例子中,我們可以觀察到對Py_ssize_t設置限制的效果。不可能索引一個元素大於其大小的列表,因爲它不接受非Py_ssize_t。

關於字典數據結構,Py_ssize_t使用哈希,因爲Python沒有使用LinkedList來實現它。類似地,字典中的大小不能大於Py_ssize_t的大小。

最大尺寸

import sys
size = sys.maxsize
# creates the max length
list = range(size)
# returns the length of a list
print("The maximum length of a list:" , len(list))
print("List is created successfully")

#輸出:

# maximum size limit on a 64-bit platform
The maximum length of a list: 9223372036854775807
List is created successfully

大於最大大小

import sys
size = sys.maxsize
# handles the exception
try:
    # creates a list with maximum size + 1
    list = range(size + 1)
    # check the maximum size
    print(len(list))
    print("List is created successfully")
# exception if the size goes beyond the maximum size
except Exception as exception:
    print("Exception caught: ", exception)
    print("List is not created due to above exception")
#Python小白學習交流羣:711312441
#輸出:

# output shows exception occurs
Exception caught:  Python int too large to convert to C ssize_t
List is not created due to above exception

代碼3:該 sys.maxsize() 對比 sys.maxint 方法

sys.maxint()方法不再支持Python 3作爲整數。如果我們使用這個方法或常量,我們將得到下面的AttributeError: module 'sys' has no attribute 'maxint'。

爲了在Python 3.0中克服這個問題,引入了另一個常量sys.maxsize,我們知道它會返回Py_ssize_t的最大值。在Python 3中,int和long int是合併的。

第一個實現展示了AttributeError的示例,第二個源代碼揭示了對maxint的更好理解。

屬性錯誤

import sys
li = [20, 2, 23, 88, 3, 63, 12]
# sys.maxint is not supported in python 3. We need to use python version < 3.0
min_value = sys.maxint
for i in range(0, len(li)):
    if li[i] < min_value:
        min_value = li[i]
print("Min value : " + str(min_value))

輸出:

AttributeError: module 'sys' has no attribute 'maxint'

maxint 執行

import sys
max_int = sys.maxsize
min_int = sys.maxsize - 1
long_int = sys.maxsize + 1
print("Maximum integer size is : " + str(max_int)+" , "+str(type(max_int)))
print("Maximum integer size-1 is :" + str(max_int)+" , "+str(type(min_int)))
print("Maximum integer size+1 is :" + str(max_int)+" , "+str(type(long_int)))

#輸出:

Maximum integer size is : 9223372036854775807 , <class 'int'>
Maximum integer size-1 is :9223372036854775807 , <class 'int'>
Maximum integer size+1 is :9223372036854775807 , <class 'int'>

代碼4:在Python中使用 csv.field_size_limit(sys.maxsize)

在Python中,當我們讀取包含巨大字段的CSV文件時,它可能會拋出一個異常,說_csv.Error: field larger than field limit。適當的解決方案是不要跳過一些字段及其行。

要分析CSV,我們需要增加field_size_limit。爲此,我們需要實現以下源代碼。

import sys
# to use the field_size_limit method
import csv
maximum_Integer = sys.maxsize
while True:
    # read the csv with huge fields
    with open('myfile.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
    # Here, we reduce the size if there is an overflow error
    try:
        csv.field_size_limit(maximum_Integer)
        break
    except OverflowError:
        maximum_Integer = int(maximum_Integer/10)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章