python将字符串转换成数组_Python将字符串转换为固定列数组

如果我的问题在下面丢失了,我需要提供给家庭自动化系统的是一个数组,我可以逐个单元地检索信息。

我使用以下代码从一个轮询我的家用HVAC系统的串行设备上读取(大部分内容都是从其他人的帖子中复制得很差):

import time

import serial

import StringIO

# configure the serial connections

ser = serial.Serial(

port='/dev/ttyS0',

baudrate=9600,

parity=serial.PARITY_NONE,

stopbits=serial.STOPBITS_ONE,

bytesize=serial.EIGHTBITS

)

input=1

while 1 :

# Use static command to debug

input = "stats"

# Python 3 users

# input = input(">> ")

if input == 'exit':

ser.close()

exit()

else:

# send the character to the device

# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)

ser.write(input + '\r\n')

outputFCUData = ''

# let's wait one second before reading output (let's give device time to answer)

time.sleep(1)

while ser.inWaiting() > 0:

outputFCUData += ser.read(1)

if outputFCUData != '':

fcuArrayTemp = StringIO.StringIO(outputFCUData).read().splitlines()

fcuArrayTemp.pop(0)

fcuArrayTemp.pop(-1)

fcuArrayTemp.pop(-1)

fcuArrayTemp.pop(-1)

print fcuArrayTemp

exit()

如果我在没有任何格式的情况下轮询设备,结果将是:

stats

101 ON 070F 070F Low Heat OK 0

102 ON 069F 069F Low Heat OK 0

103 ON 069F 069F Low Heat OK 0

104 ON 069F 070F Low Heat OK 0

105 OFF 072F 064F High Heat U5 0

OK

>

>

当我在代码中pop(0)和pop(-1)删除除了我想要的5行信息之外的所有信息时。对于任何好奇的人,第一列(例如“101”)是我的fancoil名称,其次是状态,设定点,当前温度,风扇速度,模式(加热/冷却),错误代码(例如105没有t) -stat,所以它有U5 error),然后最后一列是向设备发送命令的任何错误 - 现在没有,因此为“0”。

所以我想获取该输出并将其转换为数组,因此我可以调用fcuStatus[i][j]命令将信息从单元格(i,j)中拉出来。

我从代码中获得的内容如下:

['101 ON 070F 070F Low Heat OK 0', '102 ON 069F 069F Low Heat OK 0', '103 ON 069F 069F Low Heat OK 0', '104 ON 069F 070F Low Heat OK 0', '105 OFF 072F 064F High Heat U5 0']

这是1行,5列列表。我相信我应该只需要读取该列表中的元素并将它们添加到数组中。所以我添加了代码:

for element in fcuArrayTemp

parts = element.split(' ')

print parts

所以现在我的输出是:

['101', 'ON', '', '070F', '070F', '', 'Low', '', 'Heat', 'OK', '0']

['102', 'ON', '', '069F', '069F', '', 'Low', '', 'Heat', 'OK', '0']

['103', 'ON', '', '069F', '069F', '', 'Low', '', 'Heat', 'OK', '0']

['104', 'ON', '', '069F', '069F', '', 'Low', '', 'Heat', 'OK', '0']

['105', 'OFF', '072F', '064F', '', 'High', '', 'Heat', 'U5', '0']

这与我想要的非常接近,除非有一些额外的列,因为当有一个双空白时我分裂在一个空白上。

我的代码很草率,我必须相信有更好的方法。有人可以告诉我如何获取我在outputFCUData变量中收到的字符串信息,并将其转换为功能数组而没有额外的空格吗?总会有8列,但随着风扇线圈被添加到系统中,阵列可以扩展到128+行。以上任何一种情况都是因为我不知道更好,不是因为我试图遵循一套特定的指导方针 - 任何建议都非常受欢迎。

编辑 - 哇 - 无线电 - 让我知道我需要什么 - 谢谢你!

for element in fcuArrayTemp

parts = element.split()

print parts

所以真的最后一部分是我如何获取那些有组织的列表并创建一个N行×8列矩阵?如果没有给追加的参数,则会出错。将“element”添加到要追加的项目(fcuArray.append(element))也不会让我在那里。

fcuArray = []

for element in parts:

fcuArray = fcuArray.append()

print fcuArray

再次感谢

编辑:找到一个适合我的解决方案 - 在这里发布给其他正在寻找类似内容的人。诀窍是在生成它们时将列中的每一行添加到我的数组中:

fcuArray = []

for element in fcuArrayTemp

parts = element.split()

fcuArray.append(parts)

现在,我可以通过请求行和位置来报告数组中的任何值。例如,要报告我的数组中第3个fancoil的名称,我会要求fcuArray [3] [0](即“print fcuArray [3] [0]”,它将返回“104”。

这是我的完整代码:

import time

import serial

import StringIO

import pprint

# configure the serial connections

ser = serial.Serial(

port='/dev/ttyS0',

baudrate=9600,

parity=serial.PARITY_NONE,

stopbits=serial.STOPBITS_ONE,

bytesize=serial.EIGHTBITS

)

input=1

while 1 :

# Use static command to debug

input = "stats"

# Python 3 users

# input = input(">> ")

if input == 'exit':

ser.close()

exit()

else:

# send the character to the device

# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)

ser.write(input + '\r\n')

outputFCUData = ''

# let's wait one second before reading output (let's give device time to answer)

time.sleep(1)

while ser.inWaiting() > 0:

outputFCUData += ser.read(1)

if outputFCUData != '':

fcuArrayTemp = StringIO.StringIO(outputFCUData).read().splitlines()

fcuArrayTemp.pop(0)

fcuArrayTemp.pop(-1)

fcuArrayTemp.pop(-1)

fcuArrayTemp.pop(-1)

fcuArray = []

for element in fcuArrayTemp:

parts = element.split()

fcuArray.append(parts)

print fcuArray

print fcuArray[3][0]

exit()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值