Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reading XML File
#python 3.5.2 import re lines = '''abcdefghijklmn <entry colname="1" rowname="1">a</entry> <entry morecols="5" morecolname="2" namest="2" nameend="7" rowname="1">a</entry> <entry colname="1" morerows="9" morerowname="2">b</entry> <entry morecols="5" morecolname="2" namest="2" nameend="7" rowname="2">b</entry> <entry colname="1" morerows="9" morerowname="2">b</entry> <morecols="4" morecolname="3" namest="3" nameend="7" morerows="2" morerowname="3">c</entry> <entry colname="2" rowname="3">c</entry> <entry colname="2" rowname="4">d</entry> <entry morecols="1" morecolname="2" namest="2" nameend="3" morerows="2" morerowname="5">e</entry> <entry colname="2" rowname="5">e</entry> abcdefghijklmn''' lines = lines.split('\n') last_num = "" last_index = 0 opened = False for i, line in enumerate(lines): arr = re.findall(r"rowname=\"\d+", line) arr2 = [] if arr: arr2 = arr[0].split('"') if arr2: if last_num and last_num != arr2[1]: lines[last_index] = lines[last_index].strip() + 'TEST' opened = False else: opened = True last_index = i last_num = arr2[1] else: if last_index: lines[last_index] = lines[last_index].strip() + 'TEST' opened = False if opened: lines[last_index] = lines[last_index].strip() + 'TEST' lines = "\n".join(lines) print(lines)
run
|
edit
|
history
|
help
0
Select fruits
nnn
Python3 complete func.
kill
PythonClosure2
Komalsri123
http
homework1
gj3
single_digit