Python将字符串转换为固定列数组

2024-10-04 03:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我的问题在下面的内容中丢失了,我需要向我的家庭自动化系统展示一个数组,我可以逐个单元地从中检索信息。在

我用下面的代码从一个串行设备中读取,该设备对我家的暖通空调系统进行了轮询(其中大部分都是从其他人的帖子中复制的):

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()

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

^{pr2}$

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

所以我想把这个输出转换成一个数组,这样我可以,例如,调用一个fcuStatus[i][j]命令从cell(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列,但随着fancoils被添加到系统中,阵列可以扩展到128+行。以上任何一条都是因为我不太了解,而不是因为我试图遵守一套特定的指导原则-任何建议都是非常受欢迎的。在

编辑-哇-收音机-给了我我所需要的-谢谢!在

for element in fcuArrayTemp
    parts = element.split()
    print parts

所以,最后一个问题是如何将这些有组织的列表,创建一个N行8列的矩阵?这个错误是因为没有给append参数。将“元素”添加到要附加的项中(fcuArray.append(元素)也不能让我去那里。在

fcuArray = []
for element in parts:
    fcuArray = fcuArray.append()
    print fcuArray

再次感谢

编辑:找到了一个对我有用的解决方案-张贴在这里的任何其他人,正在寻找类似的东西。诀窍是在生成列表时将它们的每一行添加到我的数组中:

fcuArray = []
for element in fcuArrayTemp
    parts = element.split()
    fcuArray.append(parts)

现在我可以通过请求行和位置来报告数组中的任何值。例如,要报告数组中第三个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()

Tags: thetoinputonserialokelementpop
1条回答
网友
1楼 · 发布于 2024-10-04 03:28:25

element.split(' ')更改为element.split()将足以删除无关的列。在

>>> for element in fcuArrayTemp:
...     print element.split()
...
['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']

相关问题 更多 >