Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find nth data in python after special char?

I have a file that contains some information in the line. I just want to print the words after the last character ∑.
How can I do this?

text:

App∑Category∑Rating∑Reviews∑Size∑Installs∑Type∑Price∑Content Rating∑Genres∑Last Updated∑Current Ver∑Android Ver
Photo Editor & Candy Camera & Grid & ScrapBook∑ART_AND_DESIGN∑4.1∑159∑19M∑10,000+∑Free∑0∑Everyone∑Art & Design∑7-Jan-18∑1.0.0∑4.0.3 and up
Coloring book moana∑ART_AND_DESIGN∑3.9∑967∑14M∑500,000+∑Free∑0∑Everyone∑Art & Design;Pretend Play∑15-Jan-18∑2.0.0∑4.0.3 and up

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def mapper(_, line):
    words = line.split('∑')

Result:
[4.0.3 and up, 4.0.3 and up]

>Solution :

An efficient method is to use rsplit with a maxsplit of 1, and take the last item:

def mapper(_, line):
    last = line.rsplit('∑', maxsplit=1)[-1]

Note that this will default to the full line if there isn’t the split character in the line.

If you want to ensure that there is a valid split:

def mapper(_, line):
    chunks = line.rsplit('∑', maxsplit=1)
    last = chunks[1] if len(chunks)==2 else None # or other default value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading