过去我使用 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"))
感谢您的帮助!
最佳答案
您只是忘记使用右括号“)”。更改为:
position0 = line.rfind('title="{}"'.format(date1.strftime("%A")))
关于python - 与 line.rfind 相关的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16097715/