np.genfromtxt 返回带有 'b' 的字符串 [英] np.genfromtxt returns string with 'b'
问题描述
我正在学习 NUMpy 的不同功能,并且我有一个虚拟数据集
你可以看到所有字符串前面都有b
.这是什么 b
以及如何删除它?
答案是字符串前的 b
表示它是一个字节对象,通常返回的是 utf-8
编码.它是一个字节对象.
要去掉,genfromtxt
中有一个参数是encoding
,设置为utf-8
即
df3 = np.genfromtxt('100 Sales Records.csv', delimiter=',',names=True, dtype=None, encoding='utf-8')
这会给你想要的结果.
I am learning about different functions of NUmpy, And I have a dummy dataset here named as 100-Sales-Records.
Now I want to read it using np.genfromtxt
. My code to read it is
df3 = np.genfromtxt('100 Sales Records.csv', delimiter=',',names=True, dtype=None)
Because it is a 'csv' file and have strings as well as float. Now the Output of
pd.DataFramge(df3).head()
is
WHich you can see that all strings have b
in front of them. WHat is this b
and how to remove it?
The answer is that b
before strings means that it is a byte object normally returned with utf-8
encoding. It is a bytes object.
To remove it, there is a parameter in genfromtxt
that is encoding
, set it to utf-8
i.e
df3 = np.genfromtxt('100 Sales Records.csv', delimiter=',',names=True, dtype=None, encoding='utf-8')
This will give you the desired results.
这篇关于np.genfromtxt 返回带有 'b' 的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!