过去我使用line.rfind查找固定变量,我的脚本运行正常。但是,现在我正在尝试使用line.rfind来查找更改的变量,我收到了一行代码的语法错误。这是我的代码。
#!usr/bin/env python
import urllib
from datetime import datetime
from datetime import timedelta
date = datetime.now()
date1 = date + timedelta(days=1)
class city :
def __init__(self, city_name, link, latitude, longitude) :
self.name = city_name
self.url = link
self.low0 = 0
self.high1 = 0
self.high2 = 0
self.low1 = 0
self.low2 = 0
self.lat = latitude
self.long = longitude
def retrieveTemps(self) :
filehandle = urllib.urlopen(self.url)
# get lines from result into array
lines = filehandle.readlines()
# (for each) loop through each line in lines
line_number = 0 # a counter for line number
for line in lines:
line_number = line_number + 1 # increment counter
# find string, position otherwise position is -1
position0 = line.rfind('title="{}"'.format(date1.strftime("%A"))
# string is found in line
if position0 > 0 :
self.low0 = lines[line_number + 4].split('&')[0].split('>')[-1]
我得到的错误说...
if position0 > 0 :
^
SyntaxError: invalid syntax
关于什么是错的任何想法?我认为它与我在这一行中所做的改变有某种关系......
position0 = line.rfind('title="{}"'.format(date1.strftime("%A"))
感谢您的帮助!
答案 0 :(得分:3)
你只是忘了使用右括号')'。改为:
position0 = line.rfind('title="{}"'.format(date1.strftime("%A")))