请帮我用这个程序将文件解析为 XML 文件

发布于 2024-08-06 09:59:51 字数 1038 浏览 1 评论 0原文

解析输入文本文件并生成 a) XML 文件和 b) SVG(也是 XML)文件。

输入文本文件(input.txt)包含全国各地的一些农产品配送中心和存储中心的描述。每行描述一个配送中心 (dcenter) 或一个存储中心,每个中心都有许多属性;每个属性名称(例如代码)均以其值用 = 分隔。

示例 (input.txt)

dcenter: code=d1, loc=San Jose, x=100, y=100, ctype=ct1
dcenter: code=d2, loc=San Ramon, x=300, y=200, ctype=ct2
storage: code=s1, locFrom=d1, x=50, y=50, rtype=rt1
storage: code=s2, locFrom=d1, x=-50,y=100, rtype=rt1

所需的程序输出:

输出 1

<?xml version="1.0"?>
<dcenters>
<dcenter code="d1">
<loc> San Jose </loc>
<x> 100 </x>
<y> 100 </y>
<ctype> ct1 </ctype>
</dcenter>
<storage code="S1">
<locFrom> d1 </locFrom>
<x> 150 </x>
<y> 150 </y>
<rtype> rt1 </rtype>
</storage>
<storage code="S2">
<locFrom> d1 </locFrom>
<x> 50 </x>
<y> 200 </y>
<rtype> rt1 </rtype>
</storage>

请帮助我编写该程序。我会非常感激。

To parse an input text file and generate a) an XML file and b) an SVG (also XML) file.

The input text file (input.txt) contains the description of a number of produce distribution centers and storage centers around the country. Each line describes either a single distribution center (dcenter) or a storage center, each with a number of properties; each property name (code for example) is separated by its value with a =.

Example (input.txt)

dcenter: code=d1, loc=San Jose, x=100, y=100, ctype=ct1
dcenter: code=d2, loc=San Ramon, x=300, y=200, ctype=ct2
storage: code=s1, locFrom=d1, x=50, y=50, rtype=rt1
storage: code=s2, locFrom=d1, x=-50,y=100, rtype=rt1

The desired Output of the program:

Output 1

<?xml version="1.0"?>
<dcenters>
<dcenter code="d1">
<loc> San Jose </loc>
<x> 100 </x>
<y> 100 </y>
<ctype> ct1 </ctype>
</dcenter>
<storage code="S1">
<locFrom> d1 </locFrom>
<x> 150 </x>
<y> 150 </y>
<rtype> rt1 </rtype>
</storage>
<storage code="S2">
<locFrom> d1 </locFrom>
<x> 50 </x>
<y> 200 </y>
<rtype> rt1 </rtype>
</storage>

Please help me with the program. I will really appreciate.

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

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

发布评论

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

评论(1

大海や 2024-08-13 09:59:51

假设输入是字符串s;要么来自直接赋值,要么来自 file.read:

s="""dcenter: code=d1, loc=San Jose, x=100, y=100, ctype=ct1
dcenter: code=d2, loc=San Ramon, x=300, y=200, ctype=ct2
storage: code=s1, locFrom=d1, x=50, y=50, rtype=rt1
storage: code=s2, locFrom=d1, x=-50,y=100, rtype=rt1"""

那么您可以这样:

print '<?xml version="1.0"?>'
print "<dcenters>"
for line in s.splitlines():
    type, fields = line.split(":")
    params = fields.split(",")
    code = params[0].split("=")[1].strip()
    print '<%s code="%s">' % (type, code)
    for p in params[1:]:
        ptype, pvalue = p.strip().split("=")
        print '<%s> %s </%s>' % (ptype, pvalue, ptype)
    print '</%s>' % type
print "</dcenters>"

不确定为什么示例输出中缺少 d2;我认为这是错误的。

Suppose the input is in string s; either from direct assignment or from file.read:

s="""dcenter: code=d1, loc=San Jose, x=100, y=100, ctype=ct1
dcenter: code=d2, loc=San Ramon, x=300, y=200, ctype=ct2
storage: code=s1, locFrom=d1, x=50, y=50, rtype=rt1
storage: code=s2, locFrom=d1, x=-50,y=100, rtype=rt1"""

Then you can this:

print '<?xml version="1.0"?>'
print "<dcenters>"
for line in s.splitlines():
    type, fields = line.split(":")
    params = fields.split(",")
    code = params[0].split("=")[1].strip()
    print '<%s code="%s">' % (type, code)
    for p in params[1:]:
        ptype, pvalue = p.strip().split("=")
        print '<%s> %s </%s>' % (ptype, pvalue, ptype)
    print '</%s>' % type
print "</dcenters>"

Not sure why d2 is missing from your sample output; I assume that's by mistake.

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