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
  • 18,571
  • 11
  • 90
  • 141
Yacine
  • 321
  • 2
  • 15

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()]
Yacine
  • 321
  • 2
  • 15
-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
  • 23,311
  • 18
  • 141
  • 164
  • My question is about special characters – Yacine 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