我正在用 python 编写程序并有一些问题(我是 100% 的 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
- 如何使用
|
将 2 个正则表达式合并为一个?这意味着它将仅使用一个正则表达式操作来摆脱9I-
和9I+
。 - len(rawData)返回的rawData的长度是byte吗?
谢谢。
最佳答案
看区别:
$ 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
>>>
关于Python:以字节为单位的正则表达式和字符串长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550230/