Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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
Python3中使用xml的新行格式_Python_Xml_Python 3.x - Fatal编程技术网

Python3中使用xml的新行格式

Python3中使用xml的新行格式,python,xml,python-3.x,Python,Xml,Python 3.x,我正在尝试编写一个程序,它将以问答的形式从xml中获取信息。当从下面的xml中获取数据并将数据放入字典时,我得到了以下结果 XML: 有没有办法在xml文件中消除新行和空格,同时保持文件的缩进不变,或者我必须在python中删除多余的行和空格 我当前用于导入数据的代码如下所示: def getQuestions(topic, difficulty, xmlFile): tree = et.parse(xmlFile) root = tree.getroot() quest

我正在尝试编写一个程序,它将以问答的形式从xml中获取信息。当从下面的xml中获取数据并将数据放入字典时,我得到了以下结果

XML:

有没有办法在xml文件中消除新行和空格,同时保持文件的缩进不变,或者我必须在python中删除多余的行和空格

我当前用于导入数据的代码如下所示:

def getQuestions(topic, difficulty, xmlFile):
    tree = et.parse(xmlFile)
    root = tree.getroot()
    questions={}
    for i in root.findall('.//*[@name="'+topic+'"]/'+difficulty+'/question'):
            #print(i.text)
            questions[i.text]=[]
            for j in i:
                questions[i.text].append(j.text)
    return questions

非常感谢您提供的任何帮助

如果字符串
s
,您可以使用
s.rstrip()
删除其右端的空白。换行符也被认为是空白。谢谢!这正是我想要的。非常欢迎您。给定一个字符串
s
,您可以使用
s.rstrip()
删除其右端的空白。换行符也被认为是空白。谢谢!这正是我想要的,不客气。
{'What is the capital of England?\n                ': ['London', 'Bristol', '1']}
def getQuestions(topic, difficulty, xmlFile):
    tree = et.parse(xmlFile)
    root = tree.getroot()
    questions={}
    for i in root.findall('.//*[@name="'+topic+'"]/'+difficulty+'/question'):
            #print(i.text)
            questions[i.text]=[]
            for j in i:
                questions[i.text].append(j.text)
    return questions