0

I have a text file full of alphanumeric. I am making the python script to get data form each line and append to variable and add that variable to XML. The XML is adding the filename to the alphanumeric. However, it does not recognize the data. The error states

 Traceback (most recent call last):
 File "./xml.py", line 13, in <module>
 fn.text = values
 File "src/lxml/etree.pyx", line 1018, in lxml.etree._Element.text.__set__
 File "src/lxml/apihelpers.pxi", line 710, in lxml.etree._setNodeText
 File "src/lxml/apihelpers.pxi", line 698, in lxml.etree._createTextNode
 File "src/lxml/apihelpers.pxi", line 1491, in lxml.etree._utf8
TypeError: Argument must be bytes or unicode, got 'list'

As the error states that it should be unicode, i add the code,

values = base64.b64encode(values)

but it gave errors

Traceback (most recent call last):
 File "./xml.py", line 10, in <module>
   values = base64.b64encode(values)
 File "/usr/lib/python2.7/base64.py", line 54, in b64encode
   encoded = binascii.b2a_base64(s)[:-1]
 TypeError: b2a_base64() argument 1 must be string or buffer, not list

I tried doing this ''' values = [ x.encode('utf-8') for x in values] '''

The result shows

<FileName>['1235362363151512d12d']</FileName>

It is not what I expect. I expect the result to be

<FileName>1235362363151512d12d</FileName>
#!/usr/bin/python

from lxml import etree as ET
root = ET.Element("alphanumeric")
file = open("alphanumeric.txt", "r")

for aline in file:
        values = aline.split()
        length = len(values)
        child = ET.SubElement(root,"child")

        fn = ET.SubElement(child, "FileName")
        fn.text = values
        tree = ET.ElementTree(root)
tree.write("filename.xml")

x = ET.parse("filename.xml")
print ET.tostring(x, pretty_print=True)

alphanumeric.txt
7dc1f0e60f11c456aa15cc3546716c17
a78212faa38ef1078b300a492997fc02
7fa8c07634f937a1fcef9180531dc2e4
723fe720cb63189ddafde5045c4a7baf
6e670a837970a1fb4161d77d5f720d318d7e4dbc
4532b6f08b0d71893394e74e591a943bbc625f1c
3752656c024284ea63421d70235ec48d76a95df3
3f5505b9fad4cd5a1b934fee505942d28682ab91
5e7663f662cedcc2c520b88928824a4c7caf5a6833f77cdb0051328d74ace1c8
640ff11e6a0f8d95ac26e28ab6cefc29615795a646e40ce63c3ddc5f55bc165a
80b3e424c96bc9bdeafaac2a8fe2f21319663604b4b324d57c065a8a804a3a00

I expect the result to be a pretty XML. But instead there are errors which is about argument which must by bytes or unicode. The variable causes error. What is the correct way and method to append the variable to xml.

9
  • Can you give a few examples of alines in your file that trigger those errors? Commented Jun 18, 2019 at 11:28
  • The error is shown above. I am referring to the Traceback Commented Jun 18, 2019 at 11:29
  • I'm not talking about the errors; I'm talking about the alines which cause the errors. Commented Jun 18, 2019 at 11:34
  • The aline S did not cause errors Only XML causes error. When I ran the script, the result only show the XML errors. Commented Jun 18, 2019 at 11:37
  • 1
    I guess you wanted to call strip() on aline instead of split(). Commented Jun 18, 2019 at 12:08

1 Answer 1

1

It seems your values = aline.split() statement is the source of the problems. Try modifying the for loop this way and see if it works:

for aline in file:       
    child = ET.SubElement(root,"child")
    fn = ET.SubElement(child, "FileName")
    fn.text = aline
    tree = ET.ElementTree(root)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.