如何在Python中确定'word'的大小 [英] How to determine 'word' size in Python

查看:236
本文介绍了如何在Python中确定'word'的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道Python中单词中的字节数。我需要这个的原因是我有从文件中读取的单词数。如果我知道单词中的字节数,则可以使用 file.read(num_bytes)函数从文件中读取适当的数量。

I need to know the number of bytes in a 'word' in Python. The reason I need this is I have the number of words I need to read from a file; if I knew the number of bytes in a word, I can use the file.read(num_bytes) function to read the appropriate amount from the file.

如何确定单词中的字节数?

How can I determine the number of bytes in a word?

推荐答案

platform.architecture 函数:

>>> import platform
>>> platform.architecture()
('64bit', '')

请注意在同一页面上注意:

Pay attention to the note on the same page:


注意在Mac OS X(以及其他平台)上,可执行文件可能是包含多种架构的通用文件。
要获得当前解释器的 64位,查询sys.maxsize属性更为可靠:

Note On Mac OS X (and perhaps other platforms), executable files may be universal files containing multiple architectures. To get at the "64-bitness" of the current interpreter, it is more reliable to query the sys.maxsize attribute:

is_64bits = sys.maxsize > 2**32


请记住,这是单词python解释器的编译大小。如果python是在32位模式下编译的,则在64位主机上可以获得32的值。

Please keep in mind that this gives the word size with which the python interpreter was compiled. You could obtain a value of 32 on a 64bit host if python was compiled in 32bit mode.

如果文件是由其他可执行文件生成的,并且您可以访问此可执行文件,您可以使用 platform.architecture 函数的第一个可选参数:

If the file is produced by a different executable and you have access to this executable, you can use the first optional argument to the platform.architecture function:

>>> p.architecture('/path/to/executable')
('32bit', '')

这篇关于如何在Python中确定'word'的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆