标签: findall

Python ElementTree模块:当使用"find","findall"方法时,如何忽略XML文件的命名空间以找到匹配的元素

我想使用"findall"方法在ElementTree模块中找到源xml文件的一些元素.

但是,源xml文件(test.xml)具有命名空间.我将部分xml文件截断为样本:

<?xml version="1.0" encoding="iso-8859-1"?>
<XML_HEADER xmlns="http://www.test.com">
    <TYPE>Updates</TYPE>
    <DATE>9/26/2012 10:30:34 AM</DATE>
    <COPYRIGHT_NOTICE>All Rights Reserved.</COPYRIGHT_NOTICE>
    <LICENSE>newlicense.htm</LICENSE>
    <DEAL_LEVEL>
        <PAID_OFF>N</PAID_OFF>
        </DEAL_LEVEL>
</XML_HEADER>
Run Code Online (Sandbox Code Playgroud)

示例python代码如下:

from xml.etree import ElementTree as ET
tree = ET.parse(r"test.xml")
el1 = tree.findall("DEAL_LEVEL/PAID_OFF") # Return None
el2 = tree.findall("{http://www.test.com}DEAL_LEVEL/{http://www.test.com}PAID_OFF") # Return <Element '{http://www.test.com}DEAL_LEVEL/PAID_OFF' at 0xb78b90>
Run Code Online (Sandbox Code Playgroud)

虽然它可以工作,因为有一个名称空间"{http://www.test.com}",在每个标记前面添加一个名称空间是非常不方便的.

使用"find","findall"等方法时,如何忽略命名空间?

python namespaces find elementtree findall

118
推荐指数
8
解决办法
6万
查看次数

列表中的findAll上的Groovy过滤条件

我尝试在列表中使用findAll构建动态过滤器.我有一个变量,只有在不为null时才需要包含在过滤器中.

 @Test
    void testSample(){
        def list = [ new Employee(age:22, isManager:false), 
                     new Employee(age:23, isManager:true), 
                     new Employee(age:22, isManager:true) ] as Set

        def var = 22;
        String query1 = "it.age == var && it.isManager == true "
        String query2 = "it.isManager == true"

        println list
        println list.findAll { var ? query1 : query2 } // Should give 1 record age = 22 and manager
        var = null;
        println list.findAll { var ? query1 : query2 } // should give 2 records-only manager …
Run Code Online (Sandbox Code Playgroud)

groovy findall

37
推荐指数
1
解决办法
7万
查看次数

C#FindAll VS Where Speed

任何人都知道列表中Where和FindAll之间的任何速度差异.我知道IEnumerable的一部分在哪里,FindAll是List的一部分,我只是好奇什么更快.

c# performance where findall

35
推荐指数
2
解决办法
2万
查看次数

美丽的汤findAll找不到它们

我正在尝试解析一个网站,并获得一些与BeautifulSoup.findAll的信息,但它找不到所有..我正在使用python3

代码是这样的

#!/usr/bin/python3

from bs4 import BeautifulSoup
from urllib.request import urlopen

page = urlopen ("http://mangafox.me/directory/")
# print (page.read ())
soup = BeautifulSoup (page.read ())

manga_img = soup.findAll ('a', {'class' : 'manga_img'}, limit=None)

for manga in manga_img:
    print (manga['href'])
Run Code Online (Sandbox Code Playgroud)

它只打印了一半......

python beautifulsoup findall python-3.x

27
推荐指数
1
解决办法
5万
查看次数

使用Roslyn查找对方法的所有引用

我正在寻找一组.cs文件来查看哪些调用a的Value属性Nullable<T>(查找所有引用).例如,这将匹配:

class Program
{
    static void Main()
    {
        int? nullable = 123;
        int value = nullable.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

我发现了Roslyn并查看了一些样本,但其中许多已经过时且API非常庞大.我该怎么做呢?

解析语法树后我陷入困境.这是我到目前为止:

public static void Analyze(string sourceCode)
{
    var tree = CSharpSyntaxTree.ParseText(sourceCode);

    tree./* ??? What goes here? */
}
Run Code Online (Sandbox Code Playgroud)

c# code-analysis findall roslyn

19
推荐指数
1
解决办法
1万
查看次数

python - 正则表达式搜索和findall

我需要在给定正则表达式的字符串中找到所有匹配项.我一直在findall()这样做,直到我遇到一个没有按照我的预期做的情况.例如:

regex = re.compile('(\d+,?)+')
s = 'There are 9,000,000 bicycles in Beijing.'

print re.search(regex, s).group(0)
> 9,000,000

print re.findall(regex, s)
> ['000']
Run Code Online (Sandbox Code Playgroud)

在这种情况下search()返回我需要的(最长匹配)但findall()行为不同,虽然文档暗示它应该是相同的:

findall()匹配所有出现的模式,而不仅仅是第一个模式search().

  • 为什么行为不同?

  • 我怎样才能实现的结果search()findall() (或别的东西)?

python regex search string-matching findall

15
推荐指数
2
解决办法
2万
查看次数

将List.FindAll()与lambda一起使用的VB.NET语法是什么?

在C#中,我一直在通用列表中执行FindAll,如下所示:

List<group.category> tlist = list.FindAll(p => p.parid == titem.catid);
Run Code Online (Sandbox Code Playgroud)

两个问题,这是执行这样的事情的适当方式,我如何将其转换为VB.Net

c# vb.net predicate generic-list findall

10
推荐指数
1
解决办法
8511
查看次数

从字符串列表中提取工资

我正在尝试从字符串列表中提取工资。我正在使用正则表达式 findall() 函数,但它返回许多空字符串以及薪水,这导致我稍后在代码中出现问题。


sal= '41 000€ à 63 000€ / an' #this is a sample string for which i have errors

regex = ' ?([0-9]* ?[0-9]?[0-9]?[0-9]?)'#this is my regex

re.findall(regex,sal)[0]
#returns '41 000' as expected but:
re.findall(regex,sal)[1]
#returns: '' 
#Desired result : '63 000'

#the whole list of matches is like this:
['41 000',
 '',
 '',
 '',
 '',
 '',
 '',
 '63 000',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '']
# I would prefer ['41 000','63 000']
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮忙吗?谢谢

python regex string list findall

10
推荐指数
1
解决办法
4649
查看次数

extbase存储库findAll()返回结果null

我有几个像这样的控制器:CategoryController和NewsController以及类别和新闻的域模型以及两者的reposirtories.

在NewsController中,我像这样执行dependencyInjection(与categoryController中的方法相同):

    /**
 * categoryRepository
 *
 * @var Tx_MyExtension_Domain_Repository_CategoryRepository
 */
protected $categoryRepository;


    /**
 * injectCategoryRepository
 *
 * @param Tx_MyExtension_Domain_Repository_CategoryRepository $CategoryRepository
 * @return void
 */
public function injectCategoryRepository(Tx_MyExtension_Domain_Repository_CategoryRepository $categoryRepository) {
    $this->categoryRepository = $categoryRepository;
}
Run Code Online (Sandbox Code Playgroud)

当我现在尝试这样的函数时:

    /**
 * action getCategoriesAjax
 *
 * @param Tx_MyExtension_Domain_Model_News
 * @return void
 */
public function getCategoriesAjaxAction() {
    $categories = $this->categoryRepository->findAll();
    $this->view->assign('categories',$categories);
}
Run Code Online (Sandbox Code Playgroud)

我得到一个空的结果.

对我来说奇怪的是,如果我在CategoryController中这样做,同样的函数就像魅力一样工作并返回数据库中的所有元素,对我来说更奇怪的是,如果我做一个$ this-> categoryRepository-> findByUid(1)我得到了正确的元素作为结果.

我还在我的categoryRepository中添加了一个测试函数:

public function test(){
  $query = $this->createQuery();
  $result = $query->execute();
  $amount = $result.count();
}
Run Code Online (Sandbox Code Playgroud)

如果我从categoryController调用此函数,我会返回正确数量的元素.如果我从我的newsController调用它,我得到"0"回...

我不明白......

我有什么想念??? 我的错误在哪里?

null repository findall extbase

9
推荐指数
2
解决办法
1万
查看次数

BeautifulSoup 有多个标签,每个标签都有一个特定的类

我正在尝试使用 beautifulsoup 来解析网站上的表格。(我无法分享网站源代码,因为它被限制使用。)

仅当数据具有以下两个具有这些特定类的标签时,我才尝试提取数据。

td, width=40%
tr, valign=top
Run Code Online (Sandbox Code Playgroud)

我这样做的原因是提取具有这些标签和类的数据。

我在这里找到了一些关于使用多个标签的讨论,但这个讨论只讨论了标签而不是类。但是,我确实尝试使用与使用列表相同的逻辑来扩展代码,但我认为我得到的不是我想要的:

 my_soup=soup.find_all(['td',{"width":"40%"},'tr',{'valign':'top'}])
Run Code Online (Sandbox Code Playgroud)

总而言之,我的查询是如何使用多个标签,每个标签在 find_all 中都有一个特定的类,以便结果“和”两个标签。

html python tags beautifulsoup findall

9
推荐指数
2
解决办法
5336
查看次数