Judie 发表于 2021-5-7 02:12:12

Python中b'1'是什么意思?


为什么前面有个b啊?这个b是个什么玩意儿?求解,谢谢宁www!

wp231957 发表于 2021-5-7 06:39:01

bytes型数据

阿奇_o 发表于 2021-5-7 09:37:36

本帖最后由 阿奇_o 于 2021-5-7 09:40 编辑

第一印象:你们都学的什么奇怪的东西呀,dtype='S'

第二反应:从结果上看,应该是 字节存储 byte ,可为啥 dtype='S' ? 后面还显示为 |S1 ?

于是去查了一下,
StackOverflow有人回答:
The |S1 and |S2 strings are data type descriptors; the first means the array holds strings of length 1, the second of length 2.
The | pipe symbol is the byteorder flag; in this case there is no byte order flag needed, so it's set to |, meaning not applicable.

官方:不推荐!
'S', 'a' zero-terminated bytes (not recommended)

等等,这个 zero-terminated bytes 又是神马鬼??
再查.....

维基百科:
In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with a value of zero, called NUL in this article).
Alternative names are C string, which refers to the C programming language and ASCIIZ (although C can use encodings other than ASCII).

其他参考:A null-terminated byte string (NTBS) is a sequence of nonzero bytes followed by a byte with value zero (the terminating null character).
Each byte in a byte string encodes one character of some character set.
For example, the character array {'\x63','\x61','\x74','\0'} is an NTBS holding the string "cat" in ASCII encoding.

总结:这种存储格式,原来是 一串非零字节序列数组,如上面例子的'\x63','\x61','\x74' 这些是ASCII码,
然后 附带跟着一个0值位(故叫 null-terminated 或 zero-terminated),即上面例子的 '\0' 。

ps: 我为啥要找这一圈。。官方都不推荐的数据类型。。
{:10_245:}

Stubborn 发表于 2021-5-7 10:37:48

阿奇_o 发表于 2021-5-7 09:37
第一印象:你们都学的什么奇怪的东西呀,dtype='S'

第二反应:从结果上看,应该是 字节存储 byte ,可为 ...

'\0' 是字符串的结束标志

Judie 发表于 2021-5-7 11:12:12

阿奇_o 发表于 2021-5-6 20:37
第一印象:你们都学的什么奇怪的东西呀,dtype='S'

第二反应:从结果上看,应该是 字节存储 byte ,可为 ...

字节存储 byte 又是什么ww

Judie 发表于 2021-5-7 11:13:12

wp231957 发表于 2021-5-6 17:39
bytes型数据

bytes型数据又是什么?为什么这里出来的会是byte型数据呢?
页: [1]
查看完整版本: Python中b'1'是什么意思?