I'm working with strings which have horizontal tabs in them and are trying to convert them to lists. I figured out how to convert the tabs into commas, but the two tabs at the beginning and end of the string are also being converted into indexes by re.sub
. This is creating an issue when using list comprehension to format the string as a list as ''
is not an int
. Is there a way forward from here? I'm not really keen on manually formatting each num_string
prior to feeding it to python.
>>> import re
>>> num_string = " 29 10 16 "
>>> print((re.sub("[\\s]{1,}", ",", x)).split(','))
['', '29', '10', '16', '']
>>> num_list = [int(i) for i in num_string]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
ValueError: invalid literal for int() with base 10: ' '
I'd just use the built in split with no params. python 3.8.2 seems to work out of the box.
num_string = " 29 10 16 "
num_string.split()
['29', '10', '16']
But, to directly answer your question, you could also check the 'truthiness' of the value while coercing into ints.
import re
num_string = " 29 10 16 "
num_list = re.sub("[\\s]{1,}", ",", num_string).split(',')
num_list = [int(i) for i in num_list if i]
[29, 10, 16]
There is probably a better way to do this, but you can manually loop through (re.sub("[\\s]{1,}", ",", x)).split(',')
Here's how I imagine it:
subList=(re.sub("[\\s]{1,}", ",", x)).split(',')
for item in subList:
if not item=='':
item=int(item)
else:
continue
You can add a condition to your list comprehension to completely exclude the items from your resultant list:
>>> num_list = [int(i) for i in num_string if i != '']
or if you'd like to replace them with a default value instead
>>> num_list = [int(i) if i != '' else -1 for i in num_string]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.