python list to format and replace data

Requesting data from a website returns a list which looks like this.


I'm trying to loop over the list and replace the :hash with my a variable that equals cats. The extra formatting from the list along with the extra punctuation and the search/replace has me stumped. Any additional help would be greatly appreciated.

Final Result Requested

So far I have the following

#!/usr/bin/env python3
import os
import requests
gw_path = ''

r = requests.get(gw_path)
text = r.text
for item in text:
   mod = item.replace(':hash', 'cats')


2 answers

  • answered 2018-10-11 19:39 Adam Mitchell

    Python has a handy string.replace() function. All you need to do is iterate over the list of URLs and call the method on each object.

    for url in urls:
        url.replace(':hash', 'cats')

  • answered 2018-10-11 19:41 seralouk

    If a is the initial list, use list comprehension:

    a = ["",
    modified_a = [i.replace(':hash', 'cats') for i in a]


    print "[%s]" % (','.join(modified_a)