要摆脱reg_ex_split_table输出的空结果,可以使用列表解析来过滤空结果。以下是一个示例代码:
import re
data = "apple, banana, , orange, , , pineapple"
result = [x.strip() for x in re.split(r',', data) if x.strip()]
print(result)
输出:
['apple', 'banana', 'orange', 'pineapple']
在上述代码中,使用正则表达式re.split()
函数将字符串data
根据逗号分隔为多个子字符串。然后,使用列表解析的方式对每个子字符串进行去除首尾空格操作,并通过条件if x.strip()
来过滤掉空字符串。最后,将过滤后的结果存储在列表result
中。