np.genfromtxt 返回带有 'b' 的字符串 [英] np.genfromtxt returns string with 'b'

查看:80
本文介绍了np.genfromtxt 返回带有 '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屋!

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