I'm using Python (and Pytumblr) and trying to extract a certain string from some returned data, but the string I am searching for includes ":" in it. Whenever I run my script I get the error:
File "myfile.py", line 22
if re.search('^ion': u'..', u'b', line) :
^
SyntaxError: invalid syntax
Here is my code:
import pytumblr
import re
returned = client.submission('blog') # get the submissions for a given blog
sch = open('returned')
for line in sch:
line = line.rstrip()
if re.search('^ion': u'..', u'b', line) :
print line
Is there another error in this code or is there a way to escape ":" that I don't know about? I'm pretty new to Python but I didn't think : needed to be escaped.
That's a syntax error because your colon is not part of the string. The single quote '
mark is closing off the string. Your first argument is being parsed as:
'^ion' - String 1: ^ion
: - Syntactical colon
u - The syntactical character u,
indicating you intend for the
following string literal to be
in unicode
'..' - String 2: ..
If you want your single quote at the end of ^ion
to be a part of the string, you either need to escape that with a backslash '^ion\':
or, alternatively, use double quotes around the string itself. Since Python accepts both single and double quotes for string literal markers, 'hello'
and "hello"
mean the same thing. Making '"hello world"'
and "'hello world'"
both legal strings.
If the regex is the pain point here, there's lots of literature and tooling out there to help. I recommend regex101