Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to parse unbullet list / tree list? #328

Closed
joycey97 opened this issue Aug 22, 2024 · 4 comments
Closed

how to parse unbullet list / tree list? #328

joycey97 opened this issue Aug 22, 2024 · 4 comments

Comments

@joycey97
Copy link

for a section inside an 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}}
}}

how to use the package to parse this part to obtain:

62.0% Singaporean
9.1% Permanent resident
71.1% Citizen/resident
74.3% Chinese
13.5% Malay
9.0% Indian
3.2% Other
28.9% Non-citizen/resident
@JJMC89
Copy link

JJMC89 commented Aug 22, 2024

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)

@joycey97
Copy link
Author

@JJMC89 Thank you. How to list all "keys" (parameter names) from infobox_tpl if there are other fields like

| ethnic_groups_year     = 2023
| religion_year          = 2020

in the Infobox?

@JJMC89
Copy link

JJMC89 commented Aug 22, 2024

param_names = [p.name for p in infobox_tpl.params]

@joycey97
Copy link
Author

param_names = [p.name for p in infobox_tpl.params]

Thank you!

Repository owner locked and limited conversation to collaborators Aug 29, 2024
@earwig earwig converted this issue into discussion #331 Aug 29, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants