Replies: 4 comments
-
import mwparserfromhell
text = """{{infobox
| ethnic_groups = {{ublist |item_style=white-space:nowrap; |{{Tree list}}
*62.0% [[Singaporean]]
*9.1% [[Permanent residency in Singapore|Permanent resident]]
**71.1% Citizen/resident
*** 74.3% [[Chinese Singaporeans|Chinese]]
*** 13.5% [[Malay Singaporeans|Malay]]
*** 9.0% [[Indian Singaporeans|Indian]]
***3.2% [[Demographics of Singapore#Ethnic groups|Other]]
*28.9% [[Demographics of Singapore#Ethnic groups|Non-citizen/resident]]
{{Tree list/end}}
}}
}}
"""
wikicode = mwparserfromhell.parse(text)
infobox_tpl = wikicode.filter_templates()[0] # select infobox
ethnic_groups = infobox_tpl.get('ethnic_groups').value # parameter value of interest
ubl_tpl = ethnic_groups.filter_templates()[0] # select {{ublist}}
ubl_stripped = ubl_tpl.get('1').value.strip_code() # stripped parameter value of interest
res = '\n'.join(l.strip() for l in ubl_stripped.splitlines()) # trim leading/trailing whitespace
print(res) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
earwig
-
@JJMC89 Thank you. How to list all "keys" (parameter names) from
in the Infobox? |
Beta Was this translation helpful? Give feedback.
0 replies
-
param_names = [p.name for p in infobox_tpl.params] |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thank you! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
for a section inside an Infobox:
how to use the package to parse this part to obtain:
Beta Was this translation helpful? Give feedback.
All reactions