使用 lxml 在 Python 中存储系统路径的 XML

发布于 2024-08-18 09:28:09 字数 758 浏览 3 评论 0原文

我正在使用 xml 文件来存储软件的配置。

这些配置之一将是系统路径,就像

> set_value = "c:\\test\\3 tests\\test"

我可以通过使用以下方式存储它:

> setting = etree.SubElement(settings,
> "setting", name=tmp_set_name, type =
> set_type , value= set_value)

如果我使用

doc.write(output_file, method='xml',encoding = 'utf-8', compression=0)

该文件将是:

<设置类型=“str”名称=“MyPath”值=“c:\ test \ 3测试\ test”/>

现在我用 etree.parse 方法再次读取它,

我获得了一个带有字符串值的 etree 子对象,但是字符串 包含

\3

字符,如果我尝试使用它再次写入 xml,它将被解释!!!!!!所以我不能再用它作为路径 也许我只是缺少一个简单的字符串操作,但我看不到它=) 你会如何以聪明的方式解决它?

这是一个示例,但是您认为在 xml 中存储路径并使用 lxml 解析它们的最佳方法是什么?

谢谢 !!

I'm using an xml file to store configurations for a software.

One of theese configurations would be a system path like

> set_value = "c:\\test\\3 tests\\test"

i can store it by using:

> setting = etree.SubElement(settings,
> "setting", name=tmp_set_name, type =
> set_type , value= set_value)

If I use

doc.write(output_file, method='xml',encoding = 'utf-8', compression=0)

the file would be:

< setting type="str" name="MyPath" value="c:\test\3 tests\test"/>

Now I read it again with the etree.parse method

I obtain an etree child object with a string value, but the string
contains the

\3

character and if i try to use it to write again to xml it will be interpreted !!!!! So i cannot use it anymore as a path
Maybe i'm only missing a simple string operation, but I cannot see it =)
How would you solve it in a smart way ?

This is an example, but what is the best way, you think to store paths in xml and parse them with lxml ?

Thank you !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2024-08-25 09:28:09

现在我又读了一遍
etree.parse方法

我获得了一个 etree 子对象,其中包含
字符串值,但字符串包含

<前><代码>\3

角色,如果我尝试用它来
再次写入xml将会是
解释了!!!!!!

我刚刚尝试过,但它没有被“解释”。解析后返回的元素属性是:

{'type': 'str', 'name': 'yowza!', 'value': 'c:\\test\\3 tests\\test'}

因此,正如您所看到的,它的工作原理正如您所期望的那样。如果你确实遇到这个问题,那么你所做的并不是你所说的。向我们展示真实的代码,或者制作一个小示例代码来演示问题并使用它。

Now I read it again with the
etree.parse method

I obtain an etree child object with a
string value, but the string contains
the

\3

character and if i try to use it to
write again to xml it will be
interpreted !!!!!

I just tried that, and it doesn't get "interpreted". The elements attributes as returned after parsed is:

{'type': 'str', 'name': 'yowza!', 'value': 'c:\\test\\3 tests\\test'}

So as you see this works just as you expected it to work. If you really have this problem, you are doing something else than what you are saying. Show us the real code, or make a small example code where you demonstrate the problem and use that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文