嵌入式python脚本中的索引越界

问题描述

我不断收到索引越界错误

output_items [0][y] = 1

output_items[0][y]=0

我已经尝试了所有我能想到的方法来修复它,但我不知道。这是 GNU Radio 中的自定义 python 块,GNU 对输出项及其输出方式做了一些奇怪的事情。如果您安装了 GNURadio,它可能会帮助您进行故障排除。任何帮助或提示都会很棒我已经与这个错误斗争了几个小时,并且已经用尽了我能想到的修复方法代码如下:

"""

Ascii to Morse code vector source

"""



import numpy as np
from gnuradio import gr


Morse = {
  # codes from https://www.itu.int/rec/R-REC-M.1677-1-200910-I/en
    "A": "1,1,1","B": "1,"C": "1,"D": "1,"E": "1","F": "1,"G": "1,"H": "1,"I": "1,"J": "1,"K": "1,"L": "1,"M": "1,"N": "1,"O": "1,"P": "1,"Q": "1,"R": "1,"S": "1,"T": "1,"U": "1,"V": "1,"W": "1,"X": "1,"Y": "1,"Z": "1," ": "0",# space
    "1": "1,"2": "1,"3": "1,"4": "1,"5": "1,"6": "1,"7": "1,"8": "1,"9": "1,"0": "1,".": "1,# period
    ",": "1,# comma
    ":": "1,# colon
    "?": "1,# question
    "'": "1,# apostrophe
    "-": "1,# dash or minus
    "/": "1,# slash
    "(": "1,# left parenthesis
    ")": "1,# right parenthesis
    "\"": "1,# quote
    "=": "1,# equals
    "+": "1,# plus
    "@": "1,# at sign (@)
  # these punctuation marks are not included in the ITU recommendation,# but are found in https://en.wikipedia.org/wiki/Morse_code
    "!": "1,# exclamation point
    "&": "1,# ampersand (also prosign for 'WAIT')
    ";": "1,# semicolon
    "_": "1,# underscore
    "$": "1,1"           # dollar sign
      }

class mc_sync_block(gr.sync_block):
    """
    Reads an input ascii message from the "Message" textBox and converts the message to morse code and repeats 
    """
    def __init__(self,Message = ''):
        gr.sync_block.__init__(self,name = "Ascii Message to Morse Code Vector Source",in_sig = None,#disables the input port on GRC block
            out_sig = [np.byte]
        )
        
    self.Message = Message # creates callback of Message
    
    
    def work(self,input_items,output_items):
        global Morse
        bit_stream = ""
        if (len (self.Message) > 0):
            for in0 in self.Message:
                # get next char
                inChar = str (in0)
                # convert to upper case
                ch = inChar.upper()
                # test for character in table
                if (not(ch in Morse)):
                    ch = "?"        # replace bad character with a '?'
                # build vector
                dots = str (Morse.get(ch))
                bit_stream += (dots + ",")    # letter space

            bit_stream += "0,0"    # finish with word space
            

            # get length of string
            len1 = len(bit_stream)
            # num of elements = (length+1) / 2
            num_elem = int((len1+1) / 2) 
            # convert and store elements in output array
            for x in range (0,len1):
                y = int(x / 2)
                if (bit_stream[x] == '1'):
                    output_items[0][y] = 1
                elif (bit_stream[x] == '0'):
                    output_items[0][y] = 0
                else:
                    continue                    # skip commas
            
        else:
            num_elem = 0

        return (num_elem)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...