Reputation: 4712
I am writing a program in python and have some question (I am 100% new to python):
import re
rawData = '7I+8I-7I-9I-8I-'
print len(rawData)
rawData = re.sub("[0-9]I\+","",rawData)
rawData = re.sub("[0-9]I\-","",rawData)
print rawData
|
? It means it will get rid of both 9I-
and 9I+
using just one regex operation.Thank you.
Upvotes: 1
Views: 2454
Reputation: 10595
See the difference:
$ python3
Python 3.1.3 (r313:86834, May 20 2011, 06:10:42)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('día') # Unicode text
3
>>>
$ python
Python 2.7.1 (r271:86832, May 20 2011, 17:19:04)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('día') # bytes
4
>>> len(u'día') # Unicode text
3
>>>
Python 3.1.3 (r313:86834, May 20 2011, 06:10:42)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len(b'día')
File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.
>>> len(b'dia')
3
>>>
Upvotes: 5
Reputation: 82048
len
refers to the number of characters when applied to a unicode string (this is nuanced, other answers flush that out more), bytes in a encoded string, items in a list (or set, or keys in a dictionary)...
rawData = re.sub("[0-9]I(\+|-)","",rawData)
Upvotes: 0