Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I parse the string?
#4
Another way
astring = 'FTP://21.105.28.15/abc/test1.txt'
astring2 = 'FTP://21.105.28.15/abc/def/test1.txt'

def remover(astring):
    astring = astring.split('/')
    for index, item in enumerate(astring):
        if item == '':
            astring.remove(item)
        elif index in (0, 1):
            astring.remove(item)
    return '/'.join(astring)

print(remover(astring))
print(remover(astring2))
output
Output:
/abc/test1.txt /abc/def/test1.txt
little adjustment can remove the file at the end as well
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
How do I parse the string? - by anna17 - Apr-10-2024, 07:07 AM
RE: How do I parse the string? - by Gribouillis - Apr-10-2024, 07:36 AM
RE: How do I parse the string? - by Pedroski55 - Apr-10-2024, 09:40 AM
RE: How do I parse the string? - by menator01 - Apr-10-2024, 10:23 AM
RE: How do I parse the string? - by DeaD_EyE - Apr-10-2024, 10:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Parse Nested JSON String in Python mmm07 4 1,647 Mar-28-2023, 06:07 PM
Last Post: snippsat
  parse String jaykappy 2 830 Dec-23-2022, 07:42 AM
Last Post: praveencqr
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,509 Apr-11-2021, 11:03 PM
Last Post: lastyle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020