There is a "string" of this type.

['https://m.sportowysklep.pl/media/wbthumb/2016/jordan/64321/buty-air-jordan-12-retro-bg-the-master-153265-013-573dba8b7b64f.jpg', 'https://m.sportowysklep.pl/media/wbthumb/2016/nike/67568/buty-air-jordan-12-retro-bg-flu-game-153265-002-574e89d68c3f5.jpg', 'https://m.sportowysklep.pl/media/wbthumb/2016/jordan/68519/buty-air-jordan-12-retro-bg-unc-153265-007-579f415fee419.jpg', 'https://m.sportowysklep.pl/media/wbthumb/2016/nike/69653/buty-air-jordan-12-retro-bg-gym-red-153265-600-579097ad76784.jpg'] 

In its appearance, it is a list, but a python, or to be more precise jinja, takes it as a string. The task is to get urls without quotes and brackets.

product.fullimage[2:-2]).split(',') - this is not an option to process like this.

    4 answers 4

    It makes no sense to convert a string to a list, in order to remove quotes. Moreover, the quotes only show you that this array is a string.

    From the writing I see that what you called the line is actually a list in which several addresses are located. If jinja treats it as a string, then it suffices to find a place where you yourself converted your list into a string.

    Any address from your list in jinja can be obtained by iteration:

     {% for my_url in my_list %} {{ my_url }} {% endfor %} 

    Another question is that the reel may be escaped by slashes (at least the Jung template will be screened) by your lines, so you need to take this into account.

    Well, on the issue. The string is listed as follows:

     my_list = list(my_string) 
    • 1- ['[', "'", 'h', 't', 't', 'p', 's', ':', '/', '/', ..., 'j', 'p', 'g', "'", ']'] —this is probably not the list the author of the question needs. The line in question explicitly contains a constant that creates Python list (Python list literal), so you should use ast.literal_eval() instead of list() . 2- The idea that the “string” is already a list (and therefore nothing needs to be converted) is good, although the author’s previous question clearly contains a loop, which may indicate that the author knows about jinja cycles (but it’s not harmful to repeat) . - jfs
    • I already read the previous question. I don’t see constants in the question line, but I see that he is trying to remove quotes and break a string by commas, that is, to get the lines he needs from the list. I suspect that he is somewhere, quite by accident, just converted this list into a string. But he laid it out, in the problem, not as a string, but as a list. - Mr Fix
    • “transformed this list into a string” —the item number one of my answer about it (that is, I also think that this is one of the possible explanations). "I don't see constants" —Do you understand the difference between ast.literal_eval("[]") and list("[]") ? In the first case, the string as a constant (literal) that specifies the list is interpreted. - jfs

    Two solutions:

    1. Find a place upstream where the list turns into a string, and either remove this transformation or give this list a name that you can later use
    2. A less desirable option than the previous item: ast.literal_eval() , but safer than eval() . Avoid calling eval() on an input that you do not control.

      If you really have a list inside a string, you can do this:

       my_list = eval(my_string) 

      But eval is a very slippery slope. And what is not satisfied with the option with split? IMHO, this is the most natural solution in this situation.

      • product.fullimage[2:-2]).split('\', \'') - this is how it works, but it seems to me not to be the best practice. - Narnik Gamarnik September
      • one
        You can use double quotes to wrap single quotes - this will do without escaping with slashes: .split ("','") - Xander
      • @Alexander: both the editing by hands of a line containing the text representation of Python-list to reach the nested lines, and the call to eval() on lines of unknown origin are a bad idea. - jfs
      • And why do you think eval slippery way? At least ONE example in the studio ... - Xyanight
      • @Xyanight eval can only be used on inputs that you have complete control over, and about which you are absolutely sure that the correct code will be there. If you skip over eval lines that other users enter, then any user can potentially execute any code on your side. That is just awful in terms of security. Well, any incorrect string in eval can drop your script. - Xander

      fn1-re.split fn2-eval, fn3-literal_eval

      As you can see, the fastest way is via re.split

       import timeit, re, ast, random STR = "'https://m.sportowysklep.pl/media/wbthumb/2016/jordan/%s/b013-573dba8b7b64f.jpg'" STR = " ,".join(STR % random.randrange(10000) for _ in range(1000)) STR = '[%s]' % STR def fn1(): def get(): for v in re.split("[' , \[ \] ]", STR): if v: yield v return list(get()) def fn2(): return eval(STR) def fn3(): return ast.literal_eval(STR) def execTime(target_: list, repeat=1): target_[:] = [(fn.__name__, timeit.Timer(fn).timeit(repeat)) for fn in target_] for e, (n, tmt) in enumerate(sorted(target_, key=lambda r: r[1]), start=1): print("{}'time {} {}".format(e, n, tmt)) execTime([fn1, fn2, fn3], 500) 

      out:

       1'time fn1 1.4746107138626618 2'time fn3 2.3419654155086693 3'time fn2 3.3087369523448684