Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将字符串转换为固定列数组_Python_String_Multidimensional Array_Serial Port - Fatal编程技术网

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

Python将字符串转换为固定列数组,python,string,multidimensional-array,serial-port,Python,String,Multidimensional Array,Serial Port,如果我的问题不在下面,我需要向我的家庭自动化系统展示的是一个数组,我可以逐个单元格地从中检索信息 我正在使用以下代码从一个串行设备读取数据,该设备轮询我的家庭HVAC系统(其中大部分是从其他人的帖子中抄袭的): 如果我在没有任何格式的情况下轮询设备,结果将是: 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 069

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

我正在使用以下代码从一个串行设备读取数据,该设备轮询我的家庭HVAC系统(其中大部分是从其他人的帖子中抄袭的):

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

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错误
),最后一列是向设备发送命令的任何错误-现在没有,因此是“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列,但随着fancoils被添加到系统中,阵列可能会扩展到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)
现在我可以通过请求行和位置来报告数组中的任何值。例如,要报告我的数组中第三个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()

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']

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']