There is a variable with a string. You need to reformat it by adding the keys '\n' and the option '[1]' is changed to a smile

 row[2] = "[1] Place chicken in a medium bowl. Cover with lime juice and water. Set aside. [2] In a blender or food processor, place allspice, nutmeg, salt, brown sugar, thyme, ginger, black pepper and vegetable oil. Blend well, then mix in onions, green onions, garlic and habanero peppers until almost smooth. [3] Pour most of the blended marinade mixture into bowl with chicken, reserving a small amount to use as a basting sauce while cooking. Cover, and marinate in the refrigerator for at least 2 hours. [4] Preheat an outdoor grill for medium heat. [5] Brush grill grate with oil. Cook chicken slowly on the preheated grill. Turn frequently, basting often with remaining marinade mixture. Cook to desired doneness." 

proper formatting:

 """ [1] Place chicken in a medium bowl. Cover with lime juice and water. Set aside. \n [2] In a blender or food processor, place allspice, nutmeg, salt, brown sugar, thyme, ginger, black pepper and vegetable oil. Blend well, then mix in onions, green onions, garlic and habanero peppers until almost smooth. \n [3] Pour most of the blended marinade mixture into bowl with chicken, reserving a small amount to use as a basting sauce while cooking. Cover, and marinate in the refrigerator for at least 2 hours. \n [4] Preheat an outdoor grill for medium heat. \n [5] Brush grill grate with oil. Cook chicken slowly on the preheated grill. Turn frequently, basting often with remaining marinade mixture. Cook to desired doneness.\n""" 

    3 answers 3

    In general, the answer is given, there is another option:

     import re def myreplace(match): return "\n{}".format(match.group()) old_s = "[1] a [2] b [3] c" p = re.compile("(\[\d+\])") new_s = p.sub(myreplaсe, old_s).strip() print new_s >>> [1] a >>> [2] b >>> [3] c 

    https://docs.python.org/2/howto/regex.html#search-and-replace


    Same with replacing with A., V., ...

     import re l = ['', 'A.', 'B.', 'C.'] def myreplace(match): s = int(match.group()[1:-1]) return "\n{}".format(l[s]) old_s = "[1] a [2] b [3] c" p = re.compile("(\[\d+\])") print p.sub(myreplace, old_s) 

    Only neatly with the list length l (or change l[s] to somefunction(s) , which changes the number to the desired string), the output:

     A. a B. b C. c 
    • fine! But is there a need to replace [1] .. with ABC ... or with some other characters? - Kill Noise
    • @Surfer, added an answer - andy.37

    Let the contents of the string in s , i.e. s = row[2] , then

     s1 = re.sub(r"(\[\d+\])", "\n\\1", s) s1 = s1.replace("\n[1]", "[1]") 

    and in s1 will be a formatted string.

    • As an option, instead of the 2nd line s1 = s1.strip() - andy.37

    if you mean one element of the array, show it as it should be, then so: row[2] = "[1] Place chicken in a medium bowl. Cover with lime juice and water. Set aside.\n [2] In a blender or food processor, place allspice, nutmeg, salt, brown sugar, thyme, ginger, black pepper and vegetable oil. Blend well, then mix in onions, green onions, garlic and habanero peppers until almost smooth.\n [3] Pour most of the blended marinade mixture into bowl with chicken, reserving a small amount to use as a basting sauce while cooking. Cover, and marinate in the refrigerator for at least 2 hours.\n [4] Preheat an outdoor grill for medium heat.\n [5] Brush grill grate with oil. Cook chicken slowly on the preheated grill. Turn frequently, basting often with remaining marinade mixture. Cook to desired doneness." If each item is an element of the array, then it is better to separate the lines by elements