我如何创建一个字符串数组numpy的? [英] How do I create a numpy array from string?

查看:4874
本文介绍了我如何创建一个字符串数组numpy的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件阅读器读取的 N 的从一个文件字节并返回字符的字符串重新presenting的(二进制)数据。我想读到的 N 的字节为数字的numpy的数组,并在其上​​运行FFT的,但我无法创建一个字符串数组。例如一对夫妇线将是真棒。

I have a file reader that reads n bytes from a file and returns a string of chars representing that (binary) data. I want to read up n bytes into a numpy array of numbers and run a FFT on it, but I'm having trouble creating an array from a string. A couple lines of example would be awesome.

编辑:
我在读原始的二进制数据,因此字符串,我得到的样子'\\ X01 \\ X05 \\ X03 \\ XFF'... 。我想这成为 [1,5,3,255]

推荐答案

您可以直接与做到这一点 numpy.fromstring

You can do this directly with numpy.fromstring:

import numpy as np
s = '\x01\x05\x03\xff'
a = np.fromstring(s, dtype='uint8')

一旦完成此, A 阵列([1,5,3,255]),你可以使用常规SciPy的/ numpy的FFT例程。

Once completing this, a is array([ 1, 5, 3, 255]) and you can use the regular scipy/numpy FFT routines.

这篇关于我如何创建一个字符串数组numpy的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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