Python3中使用xml的新行格式
我正在尝试编写一个程序,它将以问答的形式从xml中获取信息。当从下面的xml中获取数据并将数据放入字典时,我得到了以下结果 XML: 有没有办法在xml文件中消除新行和空格,同时保持文件的缩进不变,或者我必须在python中删除多余的行和空格 我当前用于导入数据的代码如下所示: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
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