0

I am trying to get the indices of capital letters (including special ones) in a line. I found here the following solution:

[i for i, c in enumerate(s) if c.isupper()]

However, this does not work for letters like: Ö, Ä and Ü: enter image description here

I tried therefore:

[re.search(r'^([^A-ZÄÖÜ]*[A-ZÄÖÜ]){i}',s).span()[1] for i in range (1,y)] 

where y is the number of capital letters in s.

The second solution works if I define i, but under the loop, it returns:

attributeerror 'nonetype' object has no attribute 'span'.

How can I solve it in an efficient way?

Delgan
  • 16,542
  • 9
  • 83
  • 127
DYEZ
  • 321
  • 2
  • 14

2 Answers2

0

The problem is that s is represented in bytes. It needs just to be decoded to unicode:

s=u'ÖÄÜ'       # str to unicode
[i for i, c in enumerate(s) if c.isupper()]
DYEZ
  • 321
  • 2
  • 14
-1

Python3: You can do that with isupper() easily, no need for regex. Unfortunately, if you are using Python2.7 this will involve some nasty encoding/decoding, which I am not so familiar with.

x = "HEY thats Some Lower Case ZÄÖÜ"
print([i for i in range(0, len(x)) if x[i].isupper() ])
>[0, 1, 2, 10, 15, 21, 26, 27, 28, 29]
user1767754
  • 21,126
  • 14
  • 125
  • 152
  • My question is about special characters – DYEZ Nov 28 '17 at 08:21
  • Special Characters or `vowel characters` like the `german Umlaut` If yes, this one works with `ä, Ä, ö, Ö, ü, Ü`as well. Oh...i am on python3 that might b the difference lemme check. – user1767754 Nov 28 '17 at 08:45