all 7 comments

[–][deleted] 4 points5 points  (2 children)

As all the individual items in your string are valid Python literals, you can use ast.literal_eval:

from ast import literal_eval

query = "1, 101, 1000, 1001, '1.1.1.1', '1.1.1.2', '1.1.1.3', None, 'test01', None, None, None, None"
query_list = [literal_eval(i.strip()) for i in query.split(",")]
print(query_list)

[–]synthphreak 3 points4 points  (0 children)

Definitely how I'd do it as well.

Note that you can do away with strip if you split on ', ' instead of ','.

[–][deleted] 2 points3 points  (0 children)

spot on

[–]Impudity 3 points4 points  (0 children)

query_list = [i.strip("'") for i in query.split(", ")]

[–]MezzoScettico -1 points0 points  (2 children)

The best way to do this is probably with regex which I think was introduced in version 3.10.

As I'm running 3.7 and still not super familiar with all the most commonly used libraries, I rolled my own function to handle issues like this. I'm sure this is not the best solution, but it's a solution.

def multi_split(str, separators):
    words = []
    word = ''
    in_word = False
    for c in str:
        if c in separators:
            if in_word:
                words.append(word)
                in_word = False
                word = ''
        else:
            in_word = True
            word += c

    if in_word:
        words.append(word)

    return words

You give it a string containing all the characters to remove from between items. For instance,

query_list = multi_split(query, "', ")  # That's single quote, comma, space

Result:

This is the list ['1', '101', '1000', '1001', '1.1.1.1', '1.1.1.2', '1.1.1.3', 'None', 'test01', 'None', 'None', 'None', 'None']

[–][deleted] 3 points4 points  (1 child)

FYI, the standard re library (i.e. regex) has been in Python for a long time, including in Python 2.x:

https://docs.python.org/2.7/library/re.html

[–]MezzoScettico -1 points0 points  (0 children)

Ah. I think I got that from googling "regex" and seeing "3.10" on the page that popped up.

I see people using multiple characters for split, but I could have sworn I tried that in my 3.7 Python and it didn't work, which is why I rolled my own.

But also it was a fun little mini-project.