python - 与 line.rfind 相关的语法错误

标签 python

过去我使用 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/

相关文章:

Python:从 html 页面解析出发布日期

python - 我正在尝试用列表来解决作业。查找列表中以 "a"开头的第二个元素

java - 在 java 中使用 python M2Crypto AES 密码

python - Python/Numpy 中的正规方程实现

python - 将 BeautifulSoup 元素解析为 Selenium

python - 无法使用非主线程在Python-OpenCV(cv2)中启动相机捕获

python - 如何在 django 中编写 urlpatterns?

python - 给定元组形式的索引列表,遍历列表列表

python - redis:达到最大客户端数

python - 在 html Flask 中迭代嵌套字典时出现 jinja2.exceptions.UndefinedError