python - Splitting a String based on tabs and spaces
问题描述
How can i split a String with delimeters being \t and ' '(space)? For example:
string="\t Hello World\t"
newString=['Hello','World']
解决方案
Use re.split
with the delimiter [\t ]+
:
string = "\t Hello World\t"
parts = re.split(r'[\t ]+', string.strip())
print(parts)
This prints:
['Hello', 'World']
Note that I strip the leading and trailing whitespace before calling re.split
. Also, if you would accept just splitting on any whitespace, we could have used re.split(r'\s+', string.strip())
instead.
推荐阅读
- html - 比较不同的 DOM 节点,哪些节点的性能更高?
- c++ - C++ 中带有数组的结构
- java - 唯一性解析为 bean 导致空值
- swift - Xcode 项目中的 Vision 和 ARKit 框架
- java - 打印没有类名的静态变量会导致错误
- rest - 在基于 symfony 的 REST API 和 Vue.js 前端中何处实施 XSS 预防
- javascript - 如何检查一个值是否为 NaN 并将其替换为 NULL?
- image-processing - 使用 SMOTE 对图像数据进行过采样
- python - 是否可以分配一个变量以在函数中使用,但它不应该是全局的
- google-cloud-platform - 1.3 亿行的 Bigtable 扫描抛出 java.lang.IllegalStateException: Not started