在Python 3中将逗号分隔的字符串转换为Numpy数组

时间:2017-04-19 07:05:54

标签: python numpy pyqtgraph

我正在使用Python在Python 3中使用PySerial从Arduino传感器读取数据。

我想使用PyQtGraph绘制数据,所以我试图将从传感器读取的字符串(288个逗号分隔值)转换为Numpy数组。但是,我尝试了几种不同的方法,并没有让它起作用。

使用此代码从传感器读取效果非常好:

#Read the line of data from the sensor
line = sensor.readline()

#Decode the line to UTF-8 and print it
lineDecoded = line.decode("UTF-8")

print(lineDecoded)

这给了我正确的输出(288个逗号分隔值):

  

137,136,151,145,141,133,145,139,140,​​145,144,139,143,141,139,136,137,138,137,127,142,135,136,137 ,134,140,​​127,141,134,128,139,135,136,180,149,147,147,151,156,140,​​153,143,143,155,163,164,192,250,277 ,282,275,258,258,248,245,231,215,225,195,195,159,186,175,168,171,173,177,185,213,224,228,231,227,219 ,261,229,231,231,250,253,262,276,269,274,274,277,276,272,291,303,351,417,483,500,473,399,315,263,255 ,239,238,244,234,231,231,242,255,272,294,293,299,314,307,306,302,310,319,304,312,327,370,464,507,514,492,425,358,327,313,299,292,291,281,259,245,232,229,224,223,222,216,226,215,211,197,202,199,197,198,193,198,185,190,196,177,198,188,183,201,193,187,159,189,184,186,185,186,185,184,196,195,200,201,198,193,241,189,186,167,179,187,174,188,180,179,169,177,173,172,175,181,175,171,180,175,176,180,184,176,190,182,172,171,179,178,174,188,175,178,167,183,171,168,174,175,171,230,175,177,159,177,170,172,171,173,168,167,169,172,168,171,177,173,167,167,171,163,170,177,172,169,167,163,157,173,161,168,174,162,1 65,171,165,162,152,165,173,158,193,161,161,147,159,161,159,169,173,168,158,161,159,158,171,167,167,155,159,169,156,159,162,157,165,161,158,147,161,171,159,

从那里开始,我想我能够将lineDecoded传递给PyQtGraph的plot函数,但是在运行这行代码之后

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288]

y = [lineDecoded]

#Plot the data
pg.plot(x, y)

我收到错误Exception: X and Y arrays must be the same shape--got (288,) and (1,).

我尝试使用各种方法(例如np.fromstring(lineDecoded)np.array(list(lineDecoded)).ravel())将字符串转换为PyQtGraph之前转换字符串,但我尝试过的方法都没有。

关于如何实现这一目标的任何想法?

编辑:我已经完成了答案,但似乎没有一个工作,每个回复的评论中都有详细信息。任何提示都会很棒。

4 个答案:

答案 0 :(得分:2)

line.decode()返回一个字符串,而不是一个列表。所以你要回到单个实体,而不是288个值。

Python有一个与字符串相关联的方法,它将接受一个字符串并将其拆分为组件部分。调用.split()方法并为其分配子字符串,在本例中为','应该做的。

#Read the line of data from the sensor
line = sensor.readline()

#Decode the line to UTF-8 and print it
lineDecoded = line.decode("UTF-8")


values = [int(i) for i in lineDecoded.split(',')]    # <<< this should work
                                                     # added a list
                                                     # comprehension to 
                                                     # convert values to integers


x = range(1,289)                   # <<< this is preferred if you need 
                                   # a range of numbers from 1 to 288

y = values

#Plot the data
pg.plot(x, y)

注意:正如@umutto在上面的注释中提到的那样,出于绘图的目的,不需要将值转换为numpy数组。列表应该没问题。

但是,如果由于某种原因你发现你想要/需要一个数组: y = np.array(值)

答案 1 :(得分:1)

使用str.split方法将行转换为list个字符串。同样@Alien建议,您应该使用range函数获取x,而不是手动枚举所有值。

y = [int(i) for i in lineDecoded.split(',')]
x = range(1,289)

import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(x, lineDecoded)
fig.show()

答案 2 :(得分:1)

复制/粘贴字符串:

In [341]: astring="""137,136,151,145,141,133,145,139,140,145,144,139,143,141,139
     ...: ,136,137,138,137,127,142,135,136,137,134,140,127,141,134,128,139,135,1
     ...: 36,180,149,147,147,151,156,140,153,143,143,155,163,164,192,250,277,282
     ...: ...
,147,159,161,159,169,173,168,158,161,159,158,171,167,167,155,159,169,1
     ...: 56,159,162,157,165,161,158,147,161,171,159"""

split(',')生成一个字符串列表。如果我将其包装在np.array中,我会得到一个字符串数组:

In [342]: np.array(astring.split(','))
Out[342]: 
array(['137', '136', '151', '145', '141', '133', '145', '139', '140',
       ...
       '162', '157', '165', '161', '158', '147', '161', '171', '159'], 
      dtype='<U3')

我需要告诉它将字符串转换为整数:

In [343]: np.array(astring.split(','),int)
Out[343]: 
array([137, 136, 151, 145, 141, 133, 145, 139, 140, 145, 144, 139, 143,
       141, 139, 136, 137, 138, 137, 127, 142, 135, 136, 137, 134, 140,
...., 165, 161, 158, 147, 161,
       171, 159])
In [344]: _.shape
Out[344]: (288,)

我第一次尝试时,字符串以逗号结束:"8,147,161,171,159,"。然后拆分包含一个空字符串,np.array无法转换为整数。我编辑了那个。我也可以使用索引[:-1]将其从字符串列表中删除。

如果它将字符串列表(在拆分后)传递给绘图函数,那就是因为该函数执行了同样的np.array(..., int)(或浮点)转换。

[int(i) for i in astring.split(',')]

可用于创建整数列表。

答案 3 :(得分:0)

export const reducer = (state, action) => {
  switch(action.type) {
    case INPUT_FIELD_UPDATE:
      return Object.assign({}, state, {
        [action.key]: action.value
      });
    default:
      return state;
  }
};