python中函数type可以测试对象类型_Python TypeError:调用类型函数时不能调用“str”对象...

我正试图将一堵墙的文本分割成文本块和文本列表,这些文本是基于按关键字分隔的标题。我想最好的方法就是递归。不幸的是,当我试图检查给定变量的类型*** TypeError: 'str' object is not callable时,出现了以下错误。当我直接调用type(var)时,PDB中也会出现同样的错误。这似乎没有道理,所以我担心这是我看不见的东西。

以下是我认为与此相关的代码部分。如果你觉得你需要看更多def separate(text,boundary = None):

pdb.set_trace()

if boundary == None:

m = re.findall(r'(?<=boundary=).*',text)

i = 0

textList = [text]

while i < len(m): #have all levels of Boundary/headers named

boundary = m[i]

textList = recursiveSplit(textList,boundary)

i += 1

return textList

def recursiveSplit(chunk,boundary):

if type(chunk) is types.ListType: #error occurs here

for object in chunk:

recursiveSplit(object,boundary)

if type(chunk) is types.StringType:

list = re.split(r'(?P)(?!--)',chunk)

return list

return None

完整代码。需要文本文件。你可以使用任何MIME电子邮件。我还将上载用于测试的电子邮件#Textbasics email parser

#based on a "show original" file converted into text

from sys import argv

import re, os, pdb, types

script, filename = argv

text = open(filename).read()

type = "text only" #Set the default type of email

#cut the email up by sections

#--A section is defined as any time there are two line breaks in a row

textList = re.split(r"\n\n", text)

header = textList[0]

if re.search(r'MIME-Version',header):

type = "MIME"

# If mail has no attachments, parse as a text-only email

class Parser(object):

def __init__(self,textList):

a = 1

self.body = ""

self.textList = textList

self.header = textList[0]

while a < len(textList):

self.body = self.body + textList[a] + '\n\n'

a += 1

m = re.search(r'(?<=Subject: ).*', self.header)

self.subject = m.group(0)

m = re.search(r'(?<=From: ).*', self.header)

self.fromVar = m.group(0)

m = re.search(r'(?<=To: ).*', self.header)

self.toVar = m.group(0)

m = re.search(r'(?<=Date: )\w+\s\w+\s\w+', self.header)

self.date = m.group(0)

def returnParsed(self,descriptor = "all"):

if descriptor == "all":

retv = "Subject: " + self.subject + "\n" + "From: " + self.fromVar + "\n" + "To: " + self.toVar + "\n" + "Date: " + self.date + "\n" + "\n" + self.body

return retv

if descriptor == "subject":

return self.subject

if descriptor == "fromVar":

return self.fromVar

if descriptor == "toVar":

return self.toVar

if descriptor == "date":

return self.date

if descriptor == "body":

return self.body

class MIMEParser(Parser):

class MIMEDataDecoder(object):

def __init__(self,decodeString,type):

pass

def __init__(self,textList):

self.textList = textList

self.nestedItems = []

newItem = NestedItem(self)

newItem.setContentType("Header")

newItem.setValue(self.textList[0])

self.nestedItems.append(newItem)

if re.search(r'(boundary=)',newItem.value):

helperItem = NestedItem(self)

helperItem.value = (self.textList[0])

m = re.search(r'(?<=Content-Type: ).+(?=;)',newItem.value)

helperItem.setContentType(m.group(0))

self.nestedItems.append(helperItem)

self.organizeData()

"""i = 0

while i < len(self.textList):

newItem = NestedItem(self)

ct = self.nextContentType

newItem.setContentType(ct)

newItem.setValue(self.textList[i])

self.nestedItems.append(newItem)

m = re.search(r'(?<=Content-Type: ).+(?=;)',self.textList[i])

if m:

self.nextContentType = m.group(0)

i += 1

"""

def nestItem (self,item):

self.nestedItems.append(item)

def organizeData(self):

self.nestLevel = 1

self.currentSuper = self

m = re.search(r'(?<=boundary=).*',self.textList[0])

self.currentBoundary = m.group(0)

self.currentList = self.textList

self.currentList.remove(self.textList[0])

self.formerObjectDatabase = {}

pdb.set_trace()

while self.nestLevel > 0:

i = 0

while i < len(self.currentList):

boundary = self.currentBoundary

#If block is a "normal block", containing a current boundary identifier

p = re.search(r'--(?P)(?!--)', text)

if p:

newItem = NestedItem(self.currentSuper)

newItem.setValue(self.currentList[i])

r = re.search(r'(?<=Content-Type: ).+(?=;)',newItem.value)

if r:

newItem.setContentType(r.group(0))

self.currentObject = newItem

self.currentSuper.nestItem(self.currentObject)

#If the block contains a new block boundary

m = re.search(r'(?<=boundary=).*',self.currentList[i])

if m:

#begin new layer of recursive commands

newFormerObject = self.FormerCurrentObject(self.currentList,self.currentSuper,self.currentBoundary)

self.formerObjectDatabase[self.nestLevel] = newFormerObject

self.currentSuper = self.currentObject

self.nestLevel += 1

self.currentBoundary = m.group(0)

boundary = self.currentBoundary

#self.currentList = re.split(r'--(?P)(?!--)', self.currentList[i])

boundary = self.currentBoundary

#If block contains an "end of boundary" marker

q = re.search(r'(?P)--', text)

if q:

self.nestLevel -= 1

currentObject = self.formerObjectDatabase[self.nestLevel]

self.currentList = currentObject.formerList

self.currentSuper = currentObject.formerSuper

self.currentBoundary = currentObject.formerBoundary

i += 1

class FormerCurrentObject:

def __init__(self,formerList,formerSuper,formerBoundary):

self.formerList = formerList

self.formerSuper = formerSuper

self.formerBoundary = formerBoundary

def printAll(self):

print "printing all: %d" % len(self.nestedItems)

i = 0

while i < len(self.nestedItems):

print "printing out item %d" % i

self.nestedItems[i].printOut()

i += 1

class NestedItem(object):

def __init__(self,superObject,contentType=" ",value = " "):

self.superObject = superObject

self.contentType = contentType

self.value = value

self.nestedItems = []

def nestItem(self,item):

self.nestedItems.append(item)

def printOut(self,printBuffer = ""):

print printBuffer + '++%s' % self.contentType

print printBuffer + self.value

a = 0

printBuffer = printBuffer + " "

while a < len(self.nestedItems):

self.nestedItems[a].printOut(printBuffer)

def setContentType(self,contentType):

self.contentType = contentType

def setValue(self,value):

self.value = value

if type == "text only":

p = Parser(textList)

print p.returnParsed()

def separate(text,boundary = None):

pdb.set_trace()

if boundary == None:

m = re.findall(r'(?<=boundary=).*',text)

i = 0

textList = [text]

while i < len(m): #have all levels of Boundary/headers named

boundary = m[i]

textList = recursiveSplit(textList,boundary)

i += 1

return textList

def recursiveSplit(chunk,boundary):

if type(chunk) is types.ListType: #<

for obj in chunk:

recursiveSplit(obj,boundary)

if type(chunk) is types.StringType:

list = re.split(r'(?P)(?!--)',chunk)

return list

return None

if type == "MIME":

#separate the text file instead by its boundary identifier

p = MIMEParser(separate(text))

p.printAll()

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值