简体   繁体   English

如何在python中找到字符串中第一个非空白字符的索引?

[英]how to find the index of the first non-whitespace character in a string in python?

Scenario: 场景:

>>> a='   Hello world'
index = 3

In this case the "H" index is '3'. 在这种情况下,“H”指数为“3”。 But I need a more general method such that for any string variable 'a' takes I need to know the index of the first character? 但是我需要一个更通用的方法,这样对于任何字符串变量'a'需要我需要知道第一个字符的索引?

Alternative scenario: 替代方案:

>>> a='\tHello world'
index = 1

If you mean the first non-whitespace character, I'd use something like this ... 如果你的意思是第一个非空白字符,我会用这样的东西......

>>> a='   Hello world'
>>> len(a) - len(a.lstrip())
3

Another one which is a little fun: 另一个有点乐趣:

>>> sum(1 for _ in itertools.takewhile(str.isspace,a))
3

But I'm willing to bet that the first version is faster as it does essentially this exact loop, only in C -- Of course, it needs to construct a new string when it's done, but that's essentially free. 但是我愿意打赌第一个版本更快,因为它基本上是这个确切的循环,只在C中 - 当然,它需要在完成时构造一个新的字符串,但这基本上是免费的。


For completeness, if the string is empty or composed of entirely whitespace, both of these will return len(a) (which is invalid if you try to index with it...) 为了完整性,如果字符串为空或由完全空格组成,则这两个字符串都将返回len(a) (如果您尝试使用它进行索引,则无效)

>>> a = "foobar"
>>> a[len(a)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

Using regex : 使用regex

>>> import re
>>> a='   Hello world'
>>> re.search(r'\S',a).start()
3
>>> a='\tHello world'
>>> re.search(r'\S',a).start()
1
>>>

Function to handle the cases when the string is empty or contains only white spaces: 当字符串为空或仅包含空格时处理案例的函数:

>>> def func(strs):
...     match = re.search(r'\S',strs)
...     if match:
...         return match.start()
...     else:
...         return 'No character found!'
...     
>>> func('\t\tfoo')
2
>>> func('   foo')
3
>>> func('     ')
'No character found!'
>>> func('')
'No character found!'

You can also try: 你也可以尝试:

a = '   Hello world'
a.index(a.lstrip()[0])
=> 3

It'll work as long as the string contains at least one non-space character. 只要字符串包含至少一个非空格字符,它就会起作用。 We can be a bit more careful and check this before: 我们可以更加小心,然后再检查一下:

a = '    '
-1 if not a or a.isspace() else a.index(a.lstrip()[0])
=> -1

Another method, just for fun... Using a special function! 另一种方法,只是为了好玩...使用特殊功能!

>>> def first_non_space_index(s):
    for idx, c in enumerate(s):
        if not c.isspace():
            return idx


>>> a = '   Hello world'        
>>> first_non_space_index(a)
3

Following mgilson's answer, you can use lstrip to strip any characters you'd like - 根据mgilson的回答,您可以使用lstrip去除您想要的任何字符 -

unwanted = ':!@#$%^&*()_+ \t\n'
a= '  _Hello world'
res = len(a) - len(a.lstrip(unwanted)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个字符串如何在python中找到所有非空白子字符串的开始和结束索引 - Given a string how to find start and end index of all non-whitespace substrings in python 如何检测在 Python 中打开的文件的每一行中的第一个非空白字符? - How can I detect the first non-whitespace character in each line of a file opened in Python? 返回Python中字符串中第一个非空白字符的最低索引 - Returning the lowest index for the first non whitespace character in a string in Python 将字符串拆分为非空白和空白列表 - Split string into list of non-whitespace and whitespace 测试非空白字符串内容 - Testing string content on non-whitespace Python中匹配非空白的正则表达式 - Regular expression for matching non-whitespace in Python 在python NLTK中,我想获取非空白字符串的形态分析结果 - In python NLTK, I want to get morphological analysis result on non-whitespace string 使用非空白分隔符拆分字符串时如何删除虚假值 - How to remove falsy values when splitting a string with a non-whitespace separator 我如何在python上找到第一个路径空白的索引 - how do i find the index of the first trail whitespace on python 获取Python中第一个不可打印字符的索引 - Get Index of the first non printable character in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM