小编jdi*_*ard的帖子

从子节点之后的 XML 节点中提取文本

我正在尝试解析包含一些文本的节点的 XML 文档,然后声明一个子节点,然后包含更多文本。例如,下面 XML 中的第二个“post”元素:

<?xml version="1.0"?>
<data>
    <post>
        this is some text
    </post>
    <post>
        here is some more text
        <quote> and a nested node </quote>
        and more text after the nested node
    </post>
</data>
Run Code Online (Sandbox Code Playgroud)

我使用以下代码尝试打印出每个节点的文本:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()

for child in root:
    print (child.text)
Run Code Online (Sandbox Code Playgroud)

但不幸的是,唯一的输出是:

this is some text
here is some more text
Run Code Online (Sandbox Code Playgroud)

请注意,我遗漏了文字and more text after the nested node

所以,

  1. 这是有效的 XML 吗?
  2. 如果是,我如何使用 ElementTree 或其他 Python XML …

python xml elementtree

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

python raw_input带有包含字符串的重音的奇怪行为

我正在编写一个程序,要求用户输入包含重音的输入.测试用户输入字符串以查看它是否与程序中声明的字符串匹配.如下所示,我的代码不起作用:


# -*- coding: utf-8 -*-

testList = ['má']
myInput = raw_input('enter something here: ')

print myInput, repr(myInput)
print testList[0], repr(testList[0])
print myInput in testList
Run Code Online (Sandbox Code Playgroud)

使用pydev在eclipse中输出

enter something here: má
m?° 'm\xe2\x88\x9a\xc2\xb0'
má 'm\xc3\xa1'
False
Run Code Online (Sandbox Code Playgroud)

IDLE输出

enter something here: má
má u'm\xe1'
má 'm\xc3\xa1'

Warning (from warnings module):
  File "/Users/ryanculkin/Desktop/delete.py", line 8
    print myInput in testList
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False
Run Code Online (Sandbox Code Playgroud)

在比较两个字符串时,如何让我的代码打印出True?

另外,我注意到在同一输入上运行此代码的结果是不同的,这取决于我是使用eclipse还是IDLE.为什么是这样?我的最终目标是将我的程序放在网上; 有什么我需要注意的,因为结果似乎是如此不稳定吗?

python unicode diacritics raw-input

3
推荐指数
1
解决办法
5536
查看次数

查找python列表中项目的排列,增加了复杂性

在我努力解释这个问题时,请耐心等待我; 我的数学生锈了,我刚开始计算机编程,抱歉!

说我有3个项目的清单.我想找到这个列表中所有可能的项目安排,每个安排由3个项目组成.

接下来,仍然使用我的原始列表,我想找到列表项目的所有可能的安排,除了我只希望安排包括两个项目.

最后,我想再次做同样的事情,除了安排只包括一个项目.

所以我期待3!+ 3!/ 1!+ 3!/ 2!或总共15个安排.只是要明确我想要的东西,如果我的列表是[1,2,3],那么代码应该产生:

1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 2
3, 1, 2
3, 2, 1

1, 2
1, 3
2, 1
2, 3
3, 1
3, 2

1
2
3
Run Code Online (Sandbox Code Playgroud)

我下面写的代码可以做我上面写的,但只适用于长度为3的列表.我可以通过添加额外的'for'循环和'elif'语句来修改代码以处理更长的列表,但我觉得必须有一种方法来概括模式.我该怎么办才能获得上述任何长度列表的排列?

我认为我详尽的枚举方法可能会让它变得比它需要的更复杂......将尝试考虑其他方法并在发现解决方案时进行更新.

def helperFunction(itemsList):

    fullPermutationsOutputList = []


    def fullPermutations(itemsList, iterations):

        for item1 in itemsList:
            if iterations == 2:
                if len([item1]) == len(set([item1])):
                    fullPermutationsOutputList.append((item1,))
            else:    
                for item2 in itemsList:
                    if iterations == 1:
                        if len([item1, item2]) == len(set([item1, …
Run Code Online (Sandbox Code Playgroud)

python list permutation

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

标签 统计

python ×3

diacritics ×1

elementtree ×1

list ×1

permutation ×1

raw-input ×1

unicode ×1

xml ×1