Skip to content
Advertisement

a bytes-like object is required, not ‘str’ while parsing XML files

I am trying to parse an xml that looks like this. I want to extract information regarding the katagorie i.e ID, parent ID etc:

JavaScript

I am trying this

JavaScript

but I get this error:

JavaScript

even though I am already using encode('utf-8') in my code. How can I get rid of this error?

Advertisement

Answer

EDIT 2 If want to find regarding nested attributes or sub-classes, there are two ways:

  1. You can use a nested loop:
JavaScript

Output:

[‘1’, ‘0’]

Here, it can also return for classes which have id and parent_id but not the name kategorie.

  1. If you want to perform the task with a bit more performance and less memory:
JavaScript

Output:

[‘1’, ‘0’]

For this method, it will return for every class and sub-class named kategorie.

EDIT 1: For the issue in comments:

JavaScript

For the above xml file, the code seems to work perfectly:

JavaScript

Output:

[‘1’, ‘0’]

Original Answer: Looks like you are looking at the wrong location for the error. The error is actually occurring at

JavaScript

fields is a list containing str and not byte, that is why it is giving you the error. I would have recommended you to change the write type from wb to w, but looking at the rest of the code, it looks like you want to write in byte.

JavaScript

encode() just converts your data to byte form.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement