python报错:TypeError: slice indices must be integers or None or have an __index__ method

声明:本文为博主原创文章,不可转载 https://blog.csdn.net/jjddss/article/details/73469104

在使用Python进行矩阵操作时,当内部含有除法时,会产生错误:

TypeError: slice indices must be integers or None or have an __index__ method

 

例如:

 

img=np.hstack((a[:,0:100/2],b[:,100/2,:])) 

由于除法/自动产生的类型是浮点型,因此出现上述错误,修正方法为,将/更改为//

代码为:

 

img=np.hstack((a[:,0:100//2],b[:,100//2,:])) 
展开阅读全文

Python报错TypeError 'object' is not callable 如何解决?

03-06

主程序mcc.py代码如下:rn[code=python]rn#coding=utf-8rnimport timernimport sysrnfrom utils.mailHelper import mailHelperrnfrom utils.excutor import executorrnfrom utils.configReader import configReaderrnreload(sys)rnsys.setdefaultencoding('utf-8')rnrnclass MCC(object):rn CONFIGPATH='_config.ini'rn KEY_COMMAND='Command'rn KEY_OPEN='Open'rn KEY_BOSS='Boss'rn KEY_TIMELIMIT='timelimit'rnrn def __init__(self):rn self.mailHelper=mailHelper()rn self.configReader=configReader(self.CONFIGPATH)rn commandDict=self.configReader.getDict(self.KEY_COMMAND)rn openDict=self.configReader.getDict(self.KEY_OPEN)rn self.timelimit=int(self.configReader.readConfig(self.KEY_BOSS,self.KEY_TIMELIMIT))rn self.excutor=executor(commandDict,openDict)rn self.toRun()rnrn def toRun(self):rn while True:rn self.run()rn time.sleep(self.timelimit)rnrn def run(self):rn mailBody=self.mailHelper.acceptMail()rn if mailBody:rn exe=self.mailHelper.analysisMail(mailBody)rn if exe:rn self.excutor.execute(exe)rnrnif __name__=='__main__':rn mcc=MCC()rn[/code]rn注:这是在网上学的一个通过收发邮件控制电脑完成简单命令的程序代码rn其他模块代码:rnconfigReader.pyrn[code=python]rn# coding=utf-8rnimport ConfigParserrnimport os,sysrnrnclass configReader(object):rn def __init__(self,configPath):rn configFile=os.path.join(sys.path[0],configPath)rn self.cReader=ConfigParser.ConfigParser()rn self.cReader.read(configFile)rnrn def readConfig(self,section,item):rn return self.cReader.get(section,item)rnrn def getDict(self,section):rn commandDict=rn items=self.cReader.items(section)rn for key,value in items:rn commandDict[key]=valuern return commandDictrn[/code]rnrnexcutor.pyrn[code=python]rn#coding=utf-8rnimport osrnimport mailHelperrnimport win32apirnfrom utils.mccLog import mccLogrnclass executor(object):rn def __init__(self,commandDict,openDict):rn self.mccLog=mccLog()rn self.mailHelper=mailHelper()rn self.commandDict=commandDictrn self.openDict=openDictrnrn def execute(self,exe):rn subject=exe['subject']rn self.mccLog.mccWriteLog(u'开始处理命令。')rn self.mailHelper.sendMail('pass','Slave')rn if subject in self.commandDict:rn self.mccLog.mccWriteLog(u'执行命令')rn try:rn command=self.commandDict[subject]rn os.system(command)rn self.mailHelper.sendMail('Success','Boss')rn self.mccLog.mccWriteLog(u'执行命令成功')rn except Exception,e:rn self.mccLog.mccError(u'执行命令失败'+str(e))rn self.mailHelper.sendMail('error','Boss',e)rn elif subject in self.openDict:rn self.mccLog.mccWriteLog(u'打开文件')rn try:rn openFile=self.openDict[subject]rn win32api.ShellExecute(0,'open',openFile,'','',1)rn self.mailHelper.sendMail('Success','Boss')rn self.mccLog.mccWriteLog(u'打开文件成功')rn except Exception,e:rn self.mccLog.mccError(u'打开文件失败'+str(e))rn self.mailHelper.sendMail('error','Boss',e)rn else:rn self.mailHelper.sendMail('error','boss','no such command')rn[/code]rnrnmailHelper.pyrn[code=python]rn#coding=utf-8rnfrom email.mime.text import MIMETextrnfrom configReader import configReaderrnfrom mccLog import mccLogrnimport poplibrnimport smtplibrnimport rernrnclass mailHelper(object):rn CONFIGPATH='_config.ini'rnrn def __init__(self):rn self.mccLog=mccLog()rn cfReader=configReader(self.CONFIGPATH)rn self.pophost=cfReader.readConfig('Slave','pophost')rn self.smtphost=cfReader.readConfig('Slave','smtphost')rn self.port=cfReader.readConfig('Slave','port')rn self.username=cfReader.readConfig('Slave','username')rn self.password=cfReader.readConfig('Slave','password')rn self.bossMail=cfReader.readConfig('Boss','mail')rn self.loginMail()rn self.configSlaveMail()rnrn def loginMail(self):rn self.mccLog.mccWriteLog(u'开始登录邮箱')rn try:rn self.pp=poplib.POP3_SSL(self.pophost)rn self.pp.set_debuglevel(0)rn self.pp.user(self.username)rn self.pp.pass_(self.password)rn self.pp.list()rn print u'登录成功!'rn self.mccLog.mccWriteLog(u'登录邮箱成功。')rn except Exception,e:rn print u'登录失败!'rn self.mccLog.mccError(u'登录邮箱失败'+str(e))rn exit()rnrn def acceptMail(self):rn self.mccLog.mccWriteLog(u'开始抓取邮件')rn try:rn ret=self.pp.list()rn mailBody=self.pp.retr(len(ret[1]))rn self.mccLog.mccWriteLog(u'抓取邮件成功')rn return mailBodyrn except Exception,e:rn self.mccLog.mccError(u'抓取邮件失败'+str(e))rn return Nonernrn def analysisMail(self,mailBody):rn self.mccLog.mccWriteLog(u'开始抓取subject和发件人')rn try:rn subject=re.search(" 'Subject:(.*?)',",str(mailBody[1]).decode('utf-8'),re.S).group(1)rn sender=re.search(" 'X-Sender:(.*?)',",str(mailBody[1]).decode('utf-8'),re.S).group(1)rn command='subject':subject,'sender':senderrn self.mccLog.mccWriteLog(u'抓取subject和发件人成功')rn return commandrn except Exception,e:rn self.mccLog.mccError(u'抓取subject和发件人失败'+str(e))rn return Nonernrn def configSlaveMail(self):rn self.mccLog.mccWriteLog(u'开始配置发件箱')rn try:rn self.handle=smtplib.SMTP(self.smtphost,self.port)rn self.handle.login(self.username,self.password)rn self.mccLog.mccWriteLog(u'发件箱配置成功')rn except Exception,e:rn self.mccLog.mccError(u'发件箱配置失败'+str(e))rn exit()rnrn def sendMail(self,subject,receiver,body='Success'):rn msg=MIMEText(body,'plain','utf-8')rn msg['Subject']=subjectrn msg['from']=self.usernamern self.mccLog.mccWriteLog(u'开始发送邮件'+'to'+receiver)rn if receiver =='Slave':rn try:rn self.handle.sendmail(self.username,self.username,msg.as_string())rn self.mccLog.mccWriteLog(u'发送邮件成功')rn return Truern except Exception,e:rn self.mccLog.mccError(u'发送邮件失败'+str(e))rn return Falsern elif receiver =='Boss':rn try:rn self.handle.sendmail(self.username,self.bossMail,msg.as_string())rn self.mccLog.mccWriteLog(u'发送邮件成功')rn except Exception,e:rn self.mccLog.mccError(u'发送邮件失败'+str(e))rn return Falsernrnif __name__ == '__main__':rn mail=mailHelper()rn body=mail.acceptMail()rn print bodyrn print mail.analysisMail(body)rn mail.sendMail('test','Boss')rn[/code]rnrnmccLog.pyrn[code=python]rn#coding:utf-8rnimport loggingrnfrom datetime import datetimernrnclass mccLog(object):rn def __init__(self):rn logging.basicConfig(level=logging.DEBUG,rn format='%(asctime)s %(levelname)s %(message)s',rn datefmt='%Y-%m-%d %H:%M:%S',rn filename=datetime.now().strftime("%Y%m%d%H%M%S")+'.log',rn filemode='a')rnrn def mccWriteLog(self,logContent):rn logging.info(logContent)rnrn def mccError(self,errorContent):rn logging.error(errorContent)rn[/code]rnrn以下是程序运行截图:rn[img=https://img-bbs.csdn.net/upload/201603/06/1457257492_846848.png][/img]rnrn求助~急~ 论坛

没有更多推荐了,返回首页