Question

i want to know, the result format of xlrd.

See the code

>>> sh.cell_value(rowx=2, colx=1)
u'Adam Gilchrist xxxxxxxxxxxxxxxxxxxxx'

Now when i try running a 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'

I get nothing.

  1. First i want to know , what is the 'u' with result.
  2. What are the result formats returned by sh.cell_value. Is it integer, string etc.
  3. Can we run regular expressions on them?
Was it helpful?

Solution

Answering your question first

  1. First i want to know , what is the 'u' with result? u is the qualifier for unicode string. So u'Adam Gilchrist xxxxxxxxxxxxxxxxxxxxx' means the test in unicode.
  2. What are the result formats returned by sh.cell_value . Is it integer , string etc.? Its unicode string
  3. Can we run regular expressions on them ? Yes you can and this is how you do
temp1=u'Adam Gilchrist xxxxxxxxxxxxxxxxxxxxx'
x=re.search(u'Adam',temp1)    
x.group()    
u'Adam'

Its only that you have to specify the pattern in unicode also.

OTHER TIPS

  1. It's a Unicode string
  2. Cell_value returns the value of the cell. The type depends on the type of the cell.
  3. Yes. You can use regular expressions on Unicode strings, but your code isn't right.

Your code passes "temp1" to re.search as a string. It does not pass the variable temp1. You want:

>>> x=re.search(u"Adam",temp1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top