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

这篇具有很好参考价值的文章主要介绍了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。为此,我们需要实现以下源代码。文章来源地址https://www.toymoban.com/news/detail-685273.html

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)

到了这里,关于Python教程:sys模块中maxsize()的方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Python基础入门篇】基本函数——os与sys模块

    os模块和sys模块 os模块负责程序与操作系统交互,提供访问操作系统的接口 sys模块负责程序与Python解释器交互,用于操控Python运行环境 待完善

    2024年01月25日
    浏览(34)
  • Python3.6统计模块statsmodels的安装教程及使用方法

    Python3.6统计模块statsmodels的安装教程及使用方法 如果你需要对数据进行深入的统计分析和建模,那么Python编程语言中的statsmodels模块会是你的不二之选。该模块提供了多种统计模型、工具和功能,用于进行统计建模、推断、预测以及数据探索。本文将为大家详细介绍Python3.6下

    2024年02月11日
    浏览(26)
  • Python 中的 multiprocessing 模块和 threading 模块有什么区别?什么情况下应该使用哪一个?解释 Python 中的 __del__ 方法的作用。有什么需要注意的地方解释

    multiprocessing 模块和 threading 模块都是用于在 Python 中进行并发编程的工具,但它们有一些关键的区别。以下是它们之间的比较: 区别: 并行性 vs 并发性: multiprocessing 模块用于创建独立的进程,每个进程都有自己的 Python 解释器和全局解释器锁(GIL)。因此,multiprocessing 允许

    2024年02月22日
    浏览(35)
  • 第十二章 sys模块

    什么是Python 解释器 当编写Python 代码时,通常都会得到一个包含Python 代码的以.py 为扩展名的文件。要运行编写的代码,就需要使用Python 解释器去执行.py 文件。因此,Python 解释器就是用来执行Python 代码的一种工具。常见的Python 解释器有以下几种: CPython:Python 的官方解释器

    2024年02月09日
    浏览(24)
  • 修改linux的/sys目录下内核参数、模块...

    ① /sys/devices 该目录下是全局设备结构体系,包含所有被发现的注册在各种总线上的各种物理设备。一般来说,所有的物理设备都按其在总线上的拓扑结构来显示,但有两个例外,即platform devices和system devices。platform devices一般是挂在芯片内部的高速或者低速总线上的各种控制

    2024年02月05日
    浏览(44)
  • Python模块:hashlib模块教程

    1.hashlib的简介 hashlib 是一个提供了一些流行的hash(摘要)算法的Python标准库.其中所包括的算法有 md5, sha1, sha224, sha256, sha384, sha512等 什么是摘要算法呢?摘要算法又称哈希算法、散列算法。它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符

    2023年04月08日
    浏览(20)
  • python之sys库

    sys.argv 命令行参数List,第一个元素是程序本身路径 sys.modules.keys() 返回所有已经导入的模块列表 sys.exc_info() 获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息 sys.exit(n) 退出程序,正常退出时exit(0) sys.hexversion 获取Python解释程序的版本值,16进制格

    2023年04月15日
    浏览(30)
  • Python标准库sys

    Python实用教程_spiritx的博客-CSDN博客 sys 模块主要负责与 Python 解释器进行交互,该模块提供了一系列用于控制 Python 运行环境的函数和变量。 对象名称 对象说明 sys.argv 命令行参数List,第一个元素是程序本身路径  sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的

    2024年02月09日
    浏览(25)
  • 用Python制作一个自动点击器,三种模块都可以

    顾名思义,Python中的自动点击器是一个简单的Python应用程序,可以按照用户的要求重复点击鼠标。不同的参数,如速度、频率和位置,可以根据用户的要求进行改变。 Python有不同的模块可用于控制键盘、鼠标等设备。因此,我们可以使用这些模块在Python中轻松创建一个自动点

    2024年02月12日
    浏览(27)
  • dxgmms2.sys蓝屏的三种解决方法

    蓝屏问题是我们经常遇到的问题,最近很多小伙伴在玩英雄联盟的时候发现了dxgmms2.sys蓝屏错误代码,不知道怎么解决。只要一玩就会出现,之前没有出现过,一般情况下肯定是第三方软件冲突导致,卸载恶意软件,更新最新的驱动就可以了。 方法一: 1、首先了解引发蓝屏

    2024年02月12日
    浏览(100)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包