Do not use the findall
function, as it will look for att tags in the whole tree. Just iterate the tree in order from top to bottom and grab the relevant elements in them.
from xml.etree import ElementTree tree = ElementTree.parse('input.xml') root = tree.getroot() for att in root: first = att.find('attval').text for subatt in att.find('children'): second = subatt.find('attval').text print('{},{}'.format(first, second))
Which gives:
$ python process.py Data,Studyval Data,Site Info,age Info,gender
Related Posts:
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- TypeError: list indices must be integers or slices, not str
- Writing a pandas DataFrame to CSV file
- Writing a pandas DataFrame to CSV file
- IndexError: too many indices for array
- ValueError : I/O operation on closed file
- How to parse XML and count instances of a particular node attribute?
- Pandas: ValueError: cannot convert float NaN to integer
- csv.Error: iterator should return strings, not bytes
- Python – Reading and writing csv files with utf-8 encoding
- Python Error io.UnsupportedOperation: not readable
- load csv into 2D matrix with numpy for plotting
- Dump a NumPy array into a csv file
- (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
- How to load a tsv file into a Pandas DataFrame?
- Difference between writerow() and writerows() methods of Python csv module
- Create a .csv file with values from a Python list
- TypeError: list indices must be integers or slices, not str
- Convert from CSV to array in Python
- ValueError: cannot index with vector containing NA / NaN values
- (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
- Converting XML to JSON using Python?
- Creating a dictionary from a csv file?
- Python import csv to list
- ValueError: x and y must be the same size
- OSError: Initializing from file failed on csv in Pandas
- append new row to old csv file python
- How to convert an XML string to a dictionary?
- Writing a dictionary to a csv file with one line for every ‘key: value’
- Python CSV error: line contains NULL byte
- Python CSV Error: sequence expected
- AttributeError: ‘float’ object has no attribute ‘split’4
- How to add pandas data to an existing csv file?
- convert csv file to list of dictionaries
- _csv.Error: field larger than field limit (131072)
- No module named urllib3
- can we use XPath with BeautifulSoup?
- CSV new-line character seen in unquoted field error
- Writing Python lists to columns in csv
- Python code to remove HTML tags from a string
- Error in Reading a csv file in pandas[CParserError: Error tokenizing data. C error: Buffer overflow caught – possible malformed input file.]
- How to read a CSV file from a URL with Python?
- How to import a csv-file into a data array?
- How do I update Anaconda?
- TypeError: ‘str’ object is not callable (Python)
- Dijkstra’s algorithm in python
- How to create a GUID/UUID in Python
- Replace ‘ with \’ in a string
- How can I install pip on Windows?
- Best way to convert string to bytes in Python 3?
- Does Python have a string ‘contains’ substring method?
- Are static class variables possible in Python?
- Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests?
- ‘method’ object is not subscriptable. Don’t know what’s wrong
- How do I copy a file in Python?
- EOFError: Ran out of input
- Get Color Palettes from ColorHunt.co in Python
- What does the Pydoc module do?
- size of NumPy array
- Best way to strip punctuation from a string
- Could not find a version that satisfies the requirement
- Removing Conda environment
- Cannot convert list to array: ValueError: only one element tensors can be converted to Python scalars
- How do I plot only a table in Matplotlib?
- Change column type in pandas
- Split by comma and strip whitespace in Python
- How to remove punctuation marks from a string in Python 3.x using .translate()?
- Calculating Covariance with Python and Numpy
- What is the easiest way to remove all packages installed by pip?
- How can I add new keys to a dictionary?
- Convert pandas data frame to series
- Decorators with parameters?
- How do I get a python program to do nothing?
- inserting characters at the start and end of a string
- How can I install Python’s pip3 on my Mac?
- Error handling in Python-MySQL
- List files ONLY in the current directory
- No module named Image
- How to fix “TypeError: len() of unsized object”
- ValueError: Unknown projection ‘3d’ (once again)
- Difference between len() and .__len__()?
- Python: AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘split’
- How to detect key presses?
- isPrime Function for Python Language
- Getting “socket.error: [Errno 61] Connection refused” python paramiko
- python setup.py uninstall
- ImportError: No module named cv2
- ./xx.py: line 1: import: command not found
- Python iteration over non-sequence
- How do I convert a string to a double in Python?
- How do I rotate an image around its center using Pygame?
- How can I copy a Python string?
- Issue with virtualenv – cannot activate
- Can I set max_retries for requests.request?
- TypeError: zip argument #2 must support iteration
- CS231n: How to calculate gradient for Softmax loss function?
- How to create a new database using SQLAlchemy?
- Python 3 integer division [duplicate]