Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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_Parsing - Fatal编程技术网

从标记进行python搜索

从标记进行python搜索,python,parsing,Python,Parsing,我需要python编程方面的帮助: 我需要一个命令,它可以从文本文件中搜索标签之间的所有单词。 例如,文本文件中有食物。我需要搜索和之间的所有单词并显示它们。 有人能帮忙吗 将文本文件加载到字符串中 使用pos1=s.find(“”) 使用pos2=s搜索。查找(“”,pos1) 然后你寻找的单词是s[pos1+len(“”):pos2] 将文本文件加载到字符串中 使用pos1=s.find(“”) 使用pos2=s搜索。查找(“”,pos1) 然后,您寻找的单词是s[pos1+len(“”):

我需要python编程方面的帮助: 我需要一个命令,它可以从文本文件中搜索标签之间的所有单词。 例如,文本文件中有
食物
。我需要搜索
之间的所有单词并显示它们。 有人能帮忙吗

  • 将文本文件加载到字符串中
  • 使用
    pos1=s.find(“”)
  • 使用
    pos2=s搜索
    。查找(“”,pos1)
  • 然后你寻找的单词是
    s[pos1+len(“”):pos2]

  • 将文本文件加载到字符串中
  • 使用
    pos1=s.find(“”)
  • 使用
    pos2=s搜索
    。查找(“”,pos1)

  • 然后,您寻找的单词是
    s[pos1+len(“”):pos2]

    看看正则表达式

    例如,如果您想要标签
    ,请尝试

    text = "text to search. <i>this</i> is the word and also <i>that</i> end"
    import re
    re.findall("<i>(.*?)</i>",text)
    
    因为你只是想提取单词

    但是,当然可以这样做:

    re.findall("<i>(.*?)</i>",text,re.DOTALL)
    
    re.findall((*?),文本,re.DOTALL)
    
    看看正则表达式

    例如,如果您想要标签
    ,请尝试

    text = "text to search. <i>this</i> is the word and also <i>that</i> end"
    import re
    re.findall("<i>(.*?)</i>",text)
    
    因为你只是想提取单词

    但是,当然可以这样做:

    re.findall("<i>(.*?)</i>",text,re.DOTALL)
    
    re.findall((*?),文本,re.DOTALL)
    
    有一个很棒的HTML/XML遍历库,名为。有了它:

    from BeautifulSoup import BeautifulStoneSoup
    soup = BeautifulStoneSoup(open('myfile.xml', 'rt').read())
    for t in soup.findAll('concept'):
       print t.string
    

    有一个很棒的HTML/XML遍历库,名为。有了它:

    from BeautifulSoup import BeautifulStoneSoup
    soup = BeautifulStoneSoup(open('myfile.xml', 'rt').read())
    for t in soup.findAll('concept'):
       print t.string
    

    如果问题的作者暗示XML,此方法不考虑带空格的注释和标记。如果问题的作者暗示XML,此方法不考虑带空格的注释和标记