我有一个xml文档,格式如下:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gsa="http://schemas.google.com/gsa/2007">
...
<entry>
<id>https://ip.ad.dr.ess:8000/feeds/diagnostics/smb://ip.ad.dr.ess/path/to/file</id>
<updated>2011-11-07T21:32:39.795Z</updated>
<app:edited xmlns:app="http://purl.org/atom/app#">2011-11-07T21:32:39.795Z</app:edited>
<link rel="self" type="application/atom+xml" href="https://ip.ad.dr.ess:8000/feeds/diagnostics"/>
<link rel="edit" type="application/atom+xml" href="https://ip.ad.dr.ess:8000/feeds/diagnostics"/>
<gsa:content name="entryID">smb://ip.ad.dr.ess/path/to/directory</gsa:content>
<gsa:content name="numCrawledURLs">7</gsa:content>
<gsa:content name="numExcludedURLs">0</gsa:content>
<gsa:content name="type">DirectoryContentData</gsa:content>
<gsa:content name="numRetrievalErrors">0</gsa:content>
</entry>
<entry>
...
</entry>
...
</feed>
Run Code Online (Sandbox Code Playgroud)
我需要entry在lxml中使用xpath 检索所有元素.我的问题是我无法弄清楚如何使用空名称空间.我尝试过以下示例,但都没有效果.请指教.
import lxml.etree as et
tree=et.fromstring(xml)
Run Code Online (Sandbox Code Playgroud)
我尝试过的各种事情是:
for node in tree.xpath('//entry'):
Run Code Online (Sandbox Code Playgroud)
要么
namespaces = {None:"http://www.w3.org/2005/Atom" ,"openSearch":"http://a9.com/-/spec/opensearchrss/1.0/" ,"gsa":"http://schemas.google.com/gsa/2007"}
for node in tree.xpath('//entry', namespaces=ns):
Run Code Online (Sandbox Code Playgroud)
要么
for node in tree.xpath('//\"{http://www.w3.org/2005/Atom}entry\"'):
Run Code Online (Sandbox Code Playgroud)
在这一点上,我只是不知道该尝试什么.任何帮助是极大的赞赏.
我想知道JavaScript是否具有增强的for循环语法,允许您迭代数组.例如,在Java中,您可以简单地执行以下操作:
String[] array = "hello there my friend".split(" ");
for (String s : array){
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
hello
there
my
friend
Run Code Online (Sandbox Code Playgroud)
有没有办法在JavaScript中执行此操作?或者我必须使用array.length和使用标准的循环语法如下?
var array = "hello there my friend".split(" ");
for (i=0;i<array.length;i++){
document.write(array[i]);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Linux Mint 17中安装MonoDevelop.我已经安装了使用apt和通过他的包管理器,但我得到了相同的结果.MonoDevelop根本无法启动.我尝试从命令行运行它,我没有输出,也没有启动画面.什么都没发生.
任何想法如何解决这一问题?
我有一个条件语句,我需要执行两个操作之一,然后在解决了哪个操作后继续.所以我的代码目前看起来如下:
if (shoud_do_thing_a) { //should_do_thing_a is just a variable that determines which function to call. it is not a promise
do_thing_a()
} else {
do_thing_b()
}
// more code
Run Code Online (Sandbox Code Playgroud)
问题是,这两个do_thing_a和do_thing_b回用,直到两者被执行已经解决了,我不能继续前进.我想出解决这个问题的最好方法是这样的:
var more_code = function () {
// more code
}
if (shoud_do_thing_a) {
do_thing_a().then(more_code)
} else {
do_thing_b().then(more_code)
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种结构.这很难理解,因为你需要跳转才能找到more_code定义的位置(想象一下我在几个地方都有这种类型的控制流),而不是简单地继续阅读.
有没有更好的方法来处理这种类型的东西在JavaScript中?
我有一个稀疏的lua表,我需要迭代它.问题是,似乎lua从1开始迭代,并在找到nil值后立即终止.这是和例子:
> tab={}
> tab[2]='b'
> tab[5]='e'
> for i,v in ipairs(tab) do print(i,v) end
> --nothing is output here
> tab[1]='a'
> for i,v in ipairs(tab) do print(i,v) end
1 a
2 b
> --terminates after 2 (first nil value is tab[3])
Run Code Online (Sandbox Code Playgroud)
有没有办法得到所需的输出:
1 a
2 b
5 e
Run Code Online (Sandbox Code Playgroud) 说我有以下字符串:
"Hello there. My name is Fred. I am 25.5 years old."
Run Code Online (Sandbox Code Playgroud)
我想把它分成句子,所以我有以下列表:
["Hello there", "My name is Fred", "I am 25.5 years old"]
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我想在所有出现的字符串上拆分字符串". ",而不是出现任何一个"."或" ".Python str.split()在这种情况下不起作用,因为它会将字符串的每个字符视为一个单独的分隔符,而不是整个字符串作为多字符的分隔符.有没有一种简单的方法可以解决这个问题?
谢谢
编辑
愚蠢的我.Split确实以这种方式工作.
我想使用基于另一个方法引用的方法引用.这很难解释,所以我给你举个例子:
Person.java
public class Person{
Person sibling;
int age;
public Person(int age){
this.age = age;
}
public void setSibling(Person p){
this.sibling = p;
}
public Person getSibling(){
return sibling;
}
public int getAge(){
return age;
}
}
Run Code Online (Sandbox Code Playgroud)
给定一个Persons 列表,我想使用方法引用来获取其兄弟年龄的列表.我知道这可以这样做:
roster.stream().map(p -> p.getSibling().getAge()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有可能更像这样:
roster.stream().map(Person::getSibling::getAge).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
在这个例子中它并不是非常有用,我只是想知道什么是可能的.
我在cmake中配置了一个相当大的应用程序.我最近添加了几个利用C++ 0x功能的类,它打破了构建,因为cmake没有配置为使用C++ 0x支持进行编译.如何将其添加为cmake的选项?
我需要检查列表中的任何字符串是否与正则表达式匹配.如果有的话,我想继续.我过去总是这样做的方式是使用列表理解,例如:
r = re.compile('.*search.*')
if [line for line in output if r.match(line)]:
do_stuff()
Run Code Online (Sandbox Code Playgroud)
我现在意识到这是非常低效的.如果列表中的第一个项目匹配,我们可以跳过所有其余的比较并继续.我可以改进这个:
r = re.compile('.*search.*')
for line in output:
if r.match(line):
do_stuff()
break
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更多的pythonic方式来做到这一点.
是否有可用于Windows的命令行php shell?我寻找类似于python shell的东西,这将允许我打开并立即开始执行代码.