I am making a python script to create XML document and add alphanumeric from the file to XML. Before I add the data from the file to the XML document, I add the if condition if the data matches the criteria(if length is equal to 32, 40 or 64). Otherwise, I will not add that data to the XML. I also implemented a input to add data to XML. I tried to write XML and read it but there are a lot of errors.
I tried doing research on how to write XML. I just do not see any difference between my code and the tutorial.
#!/usr/bin/python
from lxml import etree as ET
from StringIO import StringIO
root = ET.Element("root")
file = open("alphanumeric.txt", "r")
invalidalhanumeric= open("invalidalhanumeric.txt", "w+")
print ("Enter the comment of the XML file: ")
comment = raw_input()
for aline in file:
values = aline.strip()
length = len(values)
if length != 32 or length != 40 or length != 64:
invalidalhanumeric.write(str(values)+ "\n")
else:
child = ET.SubElement(root,"child")
fn = ET.SubElement(child, "alphanumeric")
fn.text = values
if length == 32:
length32 = ET.SubElement(child, "length32 ")
length32 .text = values
elif length == 40:
length40 = ET.SubElement(child, "length40 ")
length40 .text = values
elif length == 64:
length64 = ET.SubElement(child, "length64 ")
length64 .text = values
rl = ET.SubElement(child, "ComplexLevel")
rl.text = "1"
cm = ET.SubElement(child, "Comment")
cm.text = comment
tree = ET.ElementTree(root)
tree.write("data.xml")
x = ET.parse(StringIO("data.xml"))
print ET.tostring(x, pretty_print=True)
alphanumeric.txt
28c806cb8c91ab66987ac1ec88344296
f6ea268c7e184f580029aec42f2a98f8
d6472dcebce348d693e68b90099d9ede
8aea2ae91cc084731a08aa231e79a430
502fbbdacada9215ed0d026c70f983e1
dd5986339aaf23f2baf8c245923a0f69
6499863d47b68030f0c5ffafaffb1344
752d245f1026482a967a763dae184569
d04f6b2157969a10c2e7421ee624075a2a5f5908
cd206f00306fb902fe25922b95da04af1028be0c
51d4b4cd19ef174a257840f3d1a419f839014f6d
62c2b7723ac79e9b009e3b0a9cb4ffa10542b9da
6e28f9ed9045abbe8321188191f92688ed064c43
93c694deec6c26acecbde4312ddbac9a0fed08e0
2a64742e32d4284640b22422c73e31ae616201be
7f0247d2f4d458ed325def12d8d7a71fc387c18a
3267f0bee5efa5dd2549722357e55fe3f4038e58
ac9fc01c1284bbe9ee4ddf424216a82b5d64a42
2197e35f14ff9960985c982ed6d16d5bd5366062
355603b1922886044884afbdfa9c9a6626b6669a
38599685f23d1840533ce5cbf5bf5114e2252435d191a3d9321093ae0bb8f88b
The result should show the XML output except this ac9fc01c1284bbe9ee4ddf424216a82b5d64a42 which does not meet the criteria. There is error messages which shows
Enter the comment of the XML file:
rmasf-231
Traceback (most recent call last):
File "./convertxml.py", line 36, in <module>
x = ET.parse(StringIO("import.xml"))
File "src/lxml/etree.pyx", line 3435, in lxml.etree.parse
File "src/lxml/parser.pxi", line 1857, in lxml.etree._parseDocument
File "src/lxml/parser.pxi", line 1877, in lxml.etree._parseMemoryDocument
File "src/lxml/parser.pxi", line 1765, in lxml.etree._parseDoc
File "src/lxml/parser.pxi", line 1127, in lxml.etree._BaseParser._parseDoc
File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult
File "src/lxml/parser.pxi", line 640, in lxml.etree._raiseParseError
File "<string>", line 1
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1
Aucun commentaire:
Enregistrer un commentaire