10 examples of 'python convert comma separated string to list' in Python

Every line of 'python convert comma separated string to list' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
118def convert_list_comma(s):
119 """
120 Return a list object from the <s>.
121 Consider <s> is comma delimited.
122 """
123 if s is None:
124 return []
125 if isinstance(s, list):
126 return s
127 return [word for word in re.split(r"\s*,\s*", s.strip()) if word != ""]
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

10@register.filter
11def list_to_comma_separated_string(l):
12 try:
13 return ', '.join(l)
14 except:
15 return None
453@staticmethod
454def comma_separated_list(x):
455 if not x:
456 return []
457 if isinstance(x, str):
458 return [t.strip() for t in x.split(",")]
459 if isinstance(x, unicode):
460 return [t.strip().encode("UTF-8") for t in x.split(u",")]
461 return list(x)
55def build_separated_list(values, sep):
56 "Make a list with sep inserted between each value in values."
57 items = []
58 if len(values):
59 for v in values[:-1]:
60 items.append((v, sep))
61 items.append(values[-1])
62 return items
164def validate_comma_separated_list(setting, value, option_parser,
165 config_parser=None, config_section=None):
166 """Check/normalize list arguments (split at "," and strip whitespace).
167 """
168 # `value` is already a list when given as command line option
169 # and "action" is "append"
170 if isinstance(value, unicode):
171 value = [value]
172 # this function is called for every option added to `value`
173 # -> split the last item and apped the result:
174 last = value.pop()
175 classes = [cls.strip(u' \t\n') for cls in last.split(',')
176 if cls.strip(u' \t\n')]
177 value.extend(classes)
178 return value
9def comma_list(list_str, item_func=None):
10 if not list_str:
11 raise ArgumentTypeError("Invalid comma list")
12 item_func = item_func or (lambda i: i)
13 return [item_func(i.strip()) for i in list_str.split(",") if i.strip()]
33def to_list(a, delimiter=',', mapping=None):
34 l = a.split(delimiter)
35 if mapping:
36 l = [mapping[li] for li in l]
37
38 return l
420def convert_list_str(string_list, type):
421 """
422 Receive a string and then converts it into a list of selected type
423 """
424 new_type_list = (string_list).split(",")
425 for inew_type_list, ele in enumerate(new_type_list):
426 if type is "int":
427 new_type_list[inew_type_list] = int(ele)
428 elif type is "float":
429 new_type_list[inew_type_list] = float(ele)
430
431 return new_type_list
131def comma_separated_ranges_to_list(string):
132 """
133 Provides a list from comma separated ranges
134
135 :param string: string of comma separated range
136 :return list: list of integer values in comma separated range
137 """
138 values = []
139 for value in string.split(','):
140 if '-' in value:
141 start, end = value.split('-')
142 for val in range(int(start), int(end) + 1):
143 values.append(int(val))
144 else:
145 values.append(int(value))
146 return values
74def split_comma_semicolon_lists(s, dtype=None):
75 """TODO
76 Parameters
77 ----------
78 s
79 Input string
80 dtype: optional
81 Data type to impose upon values
82 """
83 res = []
84 for x in s.split(";"):
85 if not ':' in x:
86 raise ValueError("Each entry must be in the form key:values,"
87 " e.g. 'targets:rest'")
88 key, s_values = x.split(':', 1)
89 values = s_values.split(',')
90 if dtype is not None:
91 values = [dtype(v) for v in values]
92 res.append((key, values))
93 return res

Related snippets