过去,我使用 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"))
谢谢您的帮助!