Python Xlrd 结果格式
我想知道,xlrd的结果格式是什么。
看看这段代码
>>> sh.cell_value(rowx=2, colx=1)
u'Adam Gilchrist xxxxxxxxxxxxxxxxxxxxx'
现在当我尝试运行res.search时
>>> temp1=sh.cell_value(rowx=2, colx=1)
>>> x=re.search("Adam",'temp1')
>>> x.group()
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
x.group()
AttributeError: 'NoneType' object has no attribute 'group'
我什么也得不到。
- 首先,我想知道,结果中的'u'是什么意思。
sh.cell_value
返回的结果格式是什么?是整数、字符串还是其他类型?- 我们可以对它们使用正则表达式吗?
2 个回答
1
- 这是一个Unicode字符串。
- Cell_value会返回单元格的值。返回的类型取决于单元格的类型。
- 是的。你可以在Unicode字符串上使用正则表达式,但你的代码有问题。
你的代码把“temp1”作为一个字符串传给了re.search,而不是传递变量temp1。你想要的是:
>>> x=re.search(u"Adam",temp1)
1
先回答你的问题
- 首先,我想知道,结果中的'u'是什么意思?'u'是用来表示unicode字符串的标记。所以
u'Adam Gilchrist xxxxxxxxxxxxxxxxxxxxx'
表示这个文本是用unicode格式的。 - sh.cell_value返回的结果格式是什么?是整数、字符串等等吗?它是unicode字符串。
- 我们可以对它们使用正则表达式吗?可以的,下面是你该怎么做。
temp1=u'Adam Gilchrist xxxxxxxxxxxxxxxxxxxxx' x=re.search(u'Adam',temp1) x.group() u'Adam'
只需要确保你指定的模式也是unicode格式的。