Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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_Xml_Tags - Fatal编程技术网

如何找到用<;指定的某个对象的字符位置;标签></>;?python

如何找到用<;指定的某个对象的字符位置;标签></>;?python,python,xml,tags,Python,Xml,Tags,我试图得到的位置,它们是真正的标记,当它们嵌入到类似于计算的东西中时 我有这样的句子: sentence = "After six weeks and seventeen tentative approaches the only serious tender came from Daniel. He had offered a paltry #2 a week for the one-time woodman's home, sane enough in this, at least, t

我试图得到
的位置,它们是真正的
标记
,当它们嵌入到类似于
计算
的东西中时

我有这样的句子:

sentence = "After six weeks and seventeen tentative approaches the only serious 
tender came from Daniel. He had offered a paltry #2 a week for the one-time 
woodman's home, sane enough in this, at least, to <tag "510270">calculate</> 
safety to the nearest new penny piece. "

sentence2 = "After six weeks and seventeen tentative approaches the only serious 
tender came from Daniel. He had offered a paltry #2 a week for the one-time 
woodman's < home, sane enough in this, at least, to <tag "510270">calculate</> 
safety to the nearest new penny > piece. "

sentence3 = "After six weeks and seventeen tentative approaches the only serious 
tender came from Daniel. He had offered a paltry #2 a week for the one-time 
woodman's > home, sane enough in this, at least, to <tag "510270">calculate</> 
safety to the nearest new penny < piece. "

如果我理解正确,这应该是可行的(假设您不更改变量
I,c

cfrom,cto=0,0
对于枚举中的i,c(第1句):
如果c==“”:
cto==i\\n从cfrom向前
打破
incfrom incto=0,0
fromtrigger,totrigger=False,False
对于enumerate中的i,c(语句1[cto:]):\\n打开标记后,查找结束标记的开始
如果c==“”:
incto=i
打破

如果我理解正确,这应该是可行的(假设您不更改变量
I,c

cfrom,cto=0,0
对于枚举中的i,c(第1句):
如果c==“”:
cto==i\\n从cfrom向前
打破
incfrom incto=0,0
fromtrigger,totrigger=False,False
对于enumerate中的i,c(语句1[cto:]):\\n打开标记后,查找结束标记的开始
如果c==“”:
incto=i
打破

下面这样的东西怎么样?当你找到标签时,你可以跟踪自己的位置:

def parseSentence(sentence):
    cfrom, cto, incfrom, incto = 0, 0, 0, 0
    place = '' #to keep track of where we are

    for i in range(len(sentence)):
        c = sentence[i]
        if (c == '<'):
            #check for 'cfrom'
            if (sentence[i : i + 4] == '<tag'):
                cfrom = i
                place = 'botag' #begin-open-tag
            #check for 'incfrom'
            elif (sentence[i + 1] == '/' and place == 'intag'):
                incfrom = i
                place = 'bctag' #begin-close-tag
        elif (c == '>'):
            #check for 'cto'
            if (place == 'botag'): #just after '<tag...'
                cto = i
                place = 'intag' #now within the XML tag
            #check for 'incto'
            elif (place == 'bctag'):
                incto = i
                place = ''
                yield (cfrom, cto, incfrom, incto)
def语法分析句子(句子):
cfrom,cto,incfrom,incto=0,0,0,0
地点=''以跟踪我们的位置
对于范围内的i(len(句子)):
c=句子[i]

如果(c==”那么下面这样的东西怎么样,当你找到标签时,你可以跟踪你在哪里:

def parseSentence(sentence):
    cfrom, cto, incfrom, incto = 0, 0, 0, 0
    place = '' #to keep track of where we are

    for i in range(len(sentence)):
        c = sentence[i]
        if (c == '<'):
            #check for 'cfrom'
            if (sentence[i : i + 4] == '<tag'):
                cfrom = i
                place = 'botag' #begin-open-tag
            #check for 'incfrom'
            elif (sentence[i + 1] == '/' and place == 'intag'):
                incfrom = i
                place = 'bctag' #begin-close-tag
        elif (c == '>'):
            #check for 'cto'
            if (place == 'botag'): #just after '<tag...'
                cto = i
                place = 'intag' #now within the XML tag
            #check for 'incto'
            elif (place == 'bctag'):
                incto = i
                place = ''
                yield (cfrom, cto, incfrom, incto)
def语法分析句子(句子):
cfrom,cto,incfrom,incto=0,0,0,0
地点=''以跟踪我们的位置
对于范围内的i(len(句子)):
c=句子[i]

如果(c==“insteag of search for”您仍然会遇到与
>
相同的问题而不是搜索”您仍然会遇到与
>
相同的问题,我想它现在对数据集是有效的。我仍然认为应该有其他更优雅的方法来做到这一点=)我想它适用于现在的数据集。我仍然认为应该有其他更优雅的方法来做到这一点=)
def parseSentence(sentence):
    cfrom, cto, incfrom, incto = 0, 0, 0, 0
    place = '' #to keep track of where we are

    for i in range(len(sentence)):
        c = sentence[i]
        if (c == '<'):
            #check for 'cfrom'
            if (sentence[i : i + 4] == '<tag'):
                cfrom = i
                place = 'botag' #begin-open-tag
            #check for 'incfrom'
            elif (sentence[i + 1] == '/' and place == 'intag'):
                incfrom = i
                place = 'bctag' #begin-close-tag
        elif (c == '>'):
            #check for 'cto'
            if (place == 'botag'): #just after '<tag...'
                cto = i
                place = 'intag' #now within the XML tag
            #check for 'incto'
            elif (place == 'bctag'):
                incto = i
                place = ''
                yield (cfrom, cto, incfrom, incto)