10 examples of 'python split number from string' in Python

Every line of 'python split number from string' 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
28def num_split(s: str) -> Tuple[Union[int, str], ...]:
29 """Splits a word into sequences of numbers and non-numbers.
30
31 Creates a tuple from these where the number are converted into int datatype.
32 That way a naturual sort can be implemented.
33 """
34 parts: List[Union[int, str]] = []
35 for part in re.split(r'(\d+)', s):
36 try:
37 parts.append(int(part))
38 except ValueError:
39 parts.append(part)
40
41 return tuple(parts)
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

143def _split_basic(string):
144 """
145 Split a string into a list of tuples of the form (key, modifier_fn,
146 explode) where modifier_fn is a function that applies the appropriate
147 modification to the variable.
148 """
149 tuples = []
150 for word in string.split(','):
151 # Attempt to split on colon
152 parts = word.split(':', 2)
153 key, modifier_fn, explode = parts[0], _identity, False
154 if len(parts) > 1:
155 modifier_fn = functools.partial(
156 _truncate, num_chars=int(parts[1]))
157 if word[len(word) - 1] == '*':
158 key = word[:len(word) - 1]
159 explode = True
160 tuples.append((key, modifier_fn, explode))
161 return tuples
68def splitnum(self, value):
69 for elem in self.cards:
70 if elem > value:
71 continue
72
73 out = []
74 if value == 0:
75 div, mod = 1, 0
76 else:
77 div, mod = divmod(value, elem)
78
79 if div == 1:
80 out.append((self.cards[1], 1))
81 else:
82 if div == value: # The system tallies, eg Roman Numerals
83 return [(div * self.cards[elem], div*elem)]
84 out.append(self.splitnum(div))
85
86 out.append((self.cards[elem], elem))
87
88 if mod:
89 out.append(self.splitnum(mod))
90
91 return out
156def get_numbers(num):
157 card_and_port = str(num)
158 card_and_port = re.split('/|-',card_and_port)
159 card = card_and_port[0]
160 fromPort = card_and_port[1]
161 toPort = fromPort if len(card_and_port) <= 2 else card_and_port[2]
162 return card, fromPort, toPort
33@staticmethod
34def split(string):
35 vendor_pos = string.rfind('-')
36 if vendor_pos != -1:
37 vendor_name = string[:vendor_pos]
38 version = string[vendor_pos+1:]
39 else:
40 vendor_name = None
41 version = string
42 return vendor_name, [int(part) for part in version.split('.')]
62def _extract_number_from(t_string):
63 return int(t_string.split()[0])
12def extractnumbers(s):
13 return tuple(map(int,re.findall("(\d+)\.(\d+)\.(\d+)",str(s))[0]))
83def test_splitNumber(self):
84 self.assertEqual(splitNumber("0222112222120000"),[0,2,2,2,1,1,2,2,2,2,1,2,0,0,0,0])
143def split_decimal(value):
144 import decimal
145 decimal.getcontext().prec = 2
146 d = decimal.Decimal(value)
147 i = int(d)
148 return (i, d - i)
7def split_string(s, n):
8 return [s[i*n:i*n+n] for i, j in enumerate(s[::n])]

Related snippets