为什么python np数组输出中有更多字符串?

2024-10-02 02:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白为什么下面的代码答案是:

import numpy as np
dt=[('name','S16'),('grad',int),('cpg',float)]
val=[("Ahmadmm",2008,12),('ali',2010,12.2),('mitra',505,15.15)]
arr=np.array(val,dtype=dt)
print(arr)

output:  [(b'Ahmadmm', 2008, 12.  ) (b'ali', 2010, 12.2 ) (b'mitra',  505, 15.15)]

为什么每个字符串前都有一个“b”


Tags: 答案代码nameimportnumpyasnpdt
2条回答

正如NPE在本question中所述,引用Python 2.x documentation

A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

报告指出:

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

我想这篇文章回答了你的问题:What does the 'b' character do in front of a string literal?。“b”字符基本上表示字节文字

相关问题 更多 >

    热门问题