我尝试创建一个这样的XML文件:
<pico:record xsi:schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd>
<dc:identifier>work_3117</dc:identifier>
</pico:record>
我使用此代码:
from lxml import etree
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd"
ns = "{xsi}"
root=etree.Element("pico:record", attrib={"{" + xsi + "}schemaLocation" : schemaLocation})
etree.SubElement(root, "dc:identifier").text = "work_3117"
print(etree.tostring(root, pretty_print=True))
结果不起作用,python告诉我:
ValueError:标记名称无效u&#39; pico:记录&#39;
如果我改变了pico:recors&#39;用&#39;记录&#39;错误是:
ValueError:无效的代码名称u&quot; dc:identifier&#39;
答案 0 :(得分:1)
好的,这个问题有点陈旧但我今天遇到了同样的问题。
你需要为代代提供“dc”的命名空间,“pico”也是如此。你必须让lxml知道这个命名空间。您可以使用在创建根元素时提供的命名空间映射来执行此操作:
from lxml import etree
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd"
pico = "http://purl.org/pico/1.0/"
dc = "http://purl.org/dc/elements/1.1/"
ns = {"xsi": xsi, "dc": dc, "pico": schemalocation}
root=etree.Element("{" + pico + "}record", attrib={"{" + xsi + "}schemaLocation" : schemaLocation}, nsmap=ns)
etree.SubElement(root, "{" + dc + "}" + "identifier").text = "work_3117"
print etree.tostring(root, pretty_print=True)
结果是:
<pico:record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pico="http://purl.org/pico/1.0/" xsi:schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd">
<dc:identifier>work_3117</dc:identifier>
</pico:record>
有关详细信息,请参阅:http://lxml.de/tutorial.html#namespaces
答案 1 :(得分:0)
在GHajba的代码中第6行有一个小故障。修正如下。
char name[9];
char surname[20];
char phone[10+1];
char buf[100];
while (fgets(buf, sizeof buf, infile)) {
int n = 0;
sscanf(buf, " %8[^ ] %19[^ ] %10[^ ]%d %n", name, surname, phone, &code, &n);
// incomplete scan or extra text detected
if (n == 0 || buf[n]) {
printf("Error line\n"); // do not use name, surname, phone, code
break;
}
printf("%s %s %s %d\n", name, surname, phone, code);
}