regular expression error ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lance Hoffmeyer

    #1

    regular expression error ?

    why isn't re.search recognizing "file"? I can print the contents of "file".


    import win32com.client
    import re

    D=MSWord.Docume nts.Open('C:/test/Test.doc')
    file=D.Content
    print file

    match = re.search('(Tab le ?)', file)
    if match:
    print "Table Number is: ", match.group(1)


    Traceback (most recent call last):
    File "test.py", line 21, in ?
    match = re.search('(Tab le ?)', file)
    File "C:\Python24\Li b\sre.py", line 134, in search
    return _compile(patter n, flags).search(s tring)
    TypeError: expected string or buffer
  • Bruno Desthuilliers

    #2
    Re: regular expression error ?

    Lance Hoffmeyer a écrit :[color=blue]
    > why isn't re.search recognizing "file"? I can print the contents of "file".
    >
    >
    > import win32com.client
    > import re
    >
    > D=MSWord.Docume nts.Open('C:/test/Test.doc')
    > file=D.Content
    > print file
    >
    > match = re.search('(Tab le ?)', file)
    > if match:
    > print "Table Number is: ", match.group(1)
    >
    >
    > Traceback (most recent call last):
    > File "test.py", line 21, in ?
    > match = re.search('(Tab le ?)', file)
    > File "C:\Python24\Li b\sre.py", line 134, in search
    > return _compile(patter n, flags).search(s tring)
    > TypeError: expected string or buffer[/color]

    The fact that you can print 'file' doesn't mean it's a string. You may
    want to try this:

    (...)
    # let's see what it really is:
    print type(D.Content) , repr(D.Content)
    # and try to make it usable for re:
    file = str(D.Content)
    (...)

    NB : not tested, I don't have Word on my Linix box !-)

    Comment

    Working...