The string:
"Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
How do I delete using map or filter all names from a string?
str = filter( x.isdigit() , for x str)
But it does not seem to work properly.
The string:
"Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
How do I delete using map or filter all names from a string?
str = filter( x.isdigit() , for x str)
But it does not seem to work properly.
How about just using split:
str = "Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
output_list = [line.split(";")[1:] for line in str.split("\n")]
output = [";".join(line) for line in output_list]
>>>['10;20;30', '20,24,25', '30;30;30', '']
You can split
the string, slice the resulting list, and then re-join
the sliced list into a string using the same delimiter:
s = "Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
print('\n'.join(';'.join(l.split(';')[1:]) for l in s.splitlines()))
This outputs:
10;20;30
20,24,25
30;30;30
In your exact case you can use python's built-in re
and list comprehension.
>>> import re
>>> text = "Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
>>> [number for number in re.split(';|,| |\n|', text) if number.isdigit()]
['10', '20', '30', '20', '24', '25', '30', '30', '30']
re.split
splits string by ;
, ,
, and
\n
List comprehension creates lists of elements that are numbers.