Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';元组';对象没有属性';高0&x27;_Python_Tuples - Fatal编程技术网

Python 属性错误:';元组';对象没有属性';高0&x27;

Python 属性错误:';元组';对象没有属性';高0&x27;,python,tuples,Python,Tuples,我正在尝试将数据列表写入excel电子表格。当我试图并行地遍历整个列表时,出现了一些问题。我得到以下错误: File "canadascript.py", line 57, in <module> sheet.write(row, high0_col, c.high0) AttributeError: 'tuple' object has no attribute 'high0' 编辑: 以下是我导入此脚本的脚本: #!usr/bin/env python import

我正在尝试将数据列表写入excel电子表格。当我试图并行地遍历整个列表时,出现了一些问题。我得到以下错误:

File "canadascript.py", line 57, in <module>
    sheet.write(row, high0_col, c.high0)
AttributeError: 'tuple' object has no attribute 'high0'
编辑:

以下是我导入此脚本的脚本:

#!usr/bin/env python

import urllib
from datetime import datetime
from datetime import timedelta

date = datetime.now()
date1 = date + timedelta(days=1)
date2 = date + timedelta(days=2)
date3 = date + timedelta(days=3)
date4 = date + timedelta(days=4)
date5 = date + timedelta(days=5)
date6 = date + timedelta(days=6)

class city :
        def __init__(self, city_name, link) :
                self.name = city_name
                self.url = link
                self.high0 = 0
                self.high1 = 0
                self.high2 = 0
                self.high3 = 0
                self.high4 = 0
                self.high5 = 0
                self.high6 = 0
                self.high7 = 0
                self.low1 = 0
                self.low2 = 0
                self.low3 = 0
                self.low4 = 0
                self.low5 = 0
                self.low6 = 0
                self.low7 = 0

        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(date.strftime("%A")))
                        position1 = line.rfind('title="{}"'.format(date1.strftime("%A")))
                        position2 = line.rfind('title="{}"'.format(date2.strftime("%A")))
                        position3 = line.rfind('title="{}"'.format(date3.strftime("%A")))
                        position4 = line.rfind('title="{}"'.format(date4.strftime("%A")))
                        position5 = line.rfind('title="{}"'.format(date5.strftime("%A")))
                        position6 = line.rfind('title="{}"'.format(date6.strftime("%A")))
                        if position0 > 0 :
                                self.high0 = lines[line_number + 4].split('&')[0].split('>')[-1]
                                self.low1 = lines[line_number + 18].split('&')[0].split('>')[-1]

                        if position1 > 0 :
                                self.high1 = lines[line_number + 4].split('&')[0].split('>')[-1]
                                self.low2 = lines[line_number + 19].split('&')[0].split('>')[-1]

                        if position2 > 0 :
                                self.high2 = lines[line_number + 4].split('&')[0].split('>')[-1]
                                self.low3 = lines[line_number + 19].split('&')[0].split('>')[-1]

                        if position3 > 0 :
                                self.high3 = lines[line_number + 4].split('&')[0].split('>')[-1]
                                self.low4 = lines[line_number + 19].split('&')[0].split('>')[-1]

                        if position4 > 0 :
                                self.high4 = lines[line_number + 4].split('&')[0].split('>')[-1]
                                self.low5 = lines[line_number + 19].split('&')[0].split('>')[-1]

                        if position5 > 0 :
                                self.high5 = lines[line_number + 4].split('&')[0].split('>')[-1]
                                self.low6 = lines[line_number + 19].split('&')[0].split('>')[-1]
                                self.low7 = lines[line_number + 19].split('&')[0].split('>')[-1]

                        if position6 > 0 :
                                self.high6 = lines[line_number + 4].split('&')[0].split('>')[-1]
                                self.high7 = lines[line_number + 4].split('&')[0].split('>')[-1]

                                break # done with loop, break out of it

                filehandle.close()

#BRITISH COLUMBIA CITIES

def getCities():

        c1 = city('Prince George', 'http://www.weatheroffice.gc.ca/city/pages/bc-79_metric_e.html')
        c2 = city('Kamloops', 'http://www.weatheroffice.gc.ca/city/pages/bc-45_metric_e.html')
        c3 = city('Blue River', 'http://www.weatheroffice.gc.ca/city/pages/bc-22_metric_e.html')
        c4 = city('High Level', 'http://www.weatheroffice.gc.ca/city/pages/ab-24_metric_e.html')
        c5 = city('Peace River', 'http://www.weatheroffice.gc.ca/city/pages/ab-25_metric_e.html')
        c6 = city('Jasper', 'http://www.weatheroffice.gc.ca/city/pages/ab-70_metric_e.html')
        c7 = city('Edmonton', 'http://www.weatheroffice.gc.ca/city/pages/ab-50_metric_e.html')
        c8 = city('Calgary', 'http://www.weatheroffice.gc.ca/city/pages/ab-52_metric_e.html')

#SASKATCHEWAN CITIES

        c9 = city('Biggar', 'http://www.weatheroffice.gc.ca/city/pages/sk-2_metric_e.html')
        c10 = city('Saskatoon', 'http://www.weatheroffice.gc.ca/city/pages/sk-40_metric_e.html')
        c11 = city('Melville', 'http://www.weatheroffice.gc.ca/city/pages/sk-8_metric_e.html')
        c12 = city('Canora', 'http://www.weatheroffice.gc.ca/city/pages/sk-3_metric_e.html')
        c13 = city('Yorkton', 'http://www.weatheroffice.gc.ca/city/pages/sk-33_metric_e.html')

#MANITOBA CITIES

        c14 = city('Winnipeg', 'http://www.weatheroffice.gc.ca/city/pages/mb-38_metric_e.html')
        c15 = city('Sprague', 'http://www.weatheroffice.gc.ca/city/pages/mb-23_metric_e.html')

#ONTARIO CITIES

        c16 = city('Thunder Bay', 'http://www.weatheroffice.gc.ca/city/pages/on-100_metric_e.html')
        c17 = city('Sioux Lookout', 'http://www.weatheroffice.gc.ca/city/pages/on-135_metric_e.html')
        c18 = city('Armstrong', 'http://www.weatheroffice.gc.ca/city/pages/on-111_metric_e.html')
        c19 = city('Hornepayne', 'http://www.weatheroffice.gc.ca/city/pages/on-78_metric_e.html')
        c20 = city('Sudbury', 'http://www.weatheroffice.gc.ca/city/pages/on-40_metric_e.html')
        c21 = city('South Parry', 'http://www.weatheroffice.gc.ca/city/pages/on-103_metric_e.html')
        c22 = city('Toronto', 'http://www.weatheroffice.gc.ca/city/pages/on-143_metric_e.html')
        c23 = city('Kingston', 'http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html')
        c24 = city('Cornwall', 'http://www.weatheroffice.gc.ca/city/pages/on-152_metric_e.html')

#QUEBEC CITIES

        c25 = city('Montreal', 'http://www.weatheroffice.gc.ca/city/pages/qc-147_metric_e.html')
        c26 = city('Quebec', 'http://www.weatheroffice.gc.ca/city/pages/qc-133_metric_e.html')
        c27 = city('La Tuque', 'http://www.weatheroffice.gc.ca/city/pages/qc-154_metric_e.html')
        c28 = city('Saguenay', 'http://www.weatheroffice.gc.ca/city/pages/qc-166_metric_e.html')
        c29 = city('Riviere-du-loup', 'http://www.weatheroffice.gc.ca/city/pages/qc-108_metric_e.html')

#NOVA SCOTIA CITIES

        c30 = city('Truro', 'http://www.weatheroffice.gc.ca/city/pages/ns-25_metric_e.html')
        c31 = city('Halifax', 'http://www.weatheroffice.gc.ca/city/pages/ns-19_metric_e.html')

#NEW BRUNSWICK CITIES

        c32 = city('Edmundston', 'http://www.weatheroffice.gc.ca/city/pages/nb-32_metric_e.html')
        c33 = city('Moncton', 'http://www.weatheroffice.gc.ca/city/pages/nb-36_metric_e.html')
        c34 = city('Sarnia', 'http://www.weatheroffice.gc.ca/city/pages/on-147_metric_e.html')

        cities = []
        cities.append(c1)
        cities.append(c2)
        cities.append(c3)
        cities.append(c4)
        cities.append(c5)
        cities.append(c6)
        cities.append(c7)
        cities.append(c8)
        cities.append(c9)
        cities.append(c10)
        cities.append(c11)
        cities.append(c12)
        cities.append(c13)
        cities.append(c14)
        cities.append(c15)
        cities.append(c16)
        cities.append(c17)
        cities.append(c18)
        cities.append(c19)
        cities.append(c20)
        cities.append(c21)
        cities.append(c22)
        cities.append(c23)
        cities.append(c24)
        cities.append(c25)
        cities.append(c26)
        cities.append(c27)
        cities.append(c28)
        cities.append(c29)
        cities.append(c30)
        cities.append(c31)
        cities.append(c32)
        cities.append(c33)
        cities.append(c34)

return (cities)

有什么想法吗?谢谢

错误出现在表达式
c.high0

如果你看一下什么是
c
,它是a的元素之一:

这意味着它是一个元组。这就是
zip
所做的:它获取序列的一个iterable,并将其转换为元组的一个iterable,其中每个元组都有每个序列的一个成员


如果您可以解释您期望每个
c
是什么,或者您为什么调用
zip
,我们可能会解释如何正确执行此操作。

错误出现在表达式
c.high0

如果你看一下什么是
c
,它是a的元素之一:

这意味着它是一个元组。这就是
zip
所做的:它获取序列的一个iterable,并将其转换为元组的一个iterable,其中每个元组都有每个序列的一个成员


如果你能解释一下你期望每个
c
是什么,或者你为什么要调用
zip
,我们可能会解释一下如何正确地使用它。

我也不明白你为什么需要使用
zip()
,但是如果你确定你需要它,就这样试试:

for c in zip(cities):
    sheet.write(row, high0_col, c[0].high0)
    sheet.write(row, low1_col, c[0].low1)
    sheet.write(row, high1_col, c[0].high1)
    sheet.write(row, low2_col, c[0].low2)
    sheet.write(row, high2_col, c[0].high2)
    sheet.write(row, low3_col, c[0].low3)
    sheet.write(row, high3_col, c[0].high3)
    sheet.write(row, low4_col, c[0].low4)
    sheet.write(row, high4_col, c[0].high4)
    sheet.write(row, low5_col, c[0].low5)
    sheet.write(row, high5_col, c[0].high5)
    sheet.write(row, low6_col, c[0].low6)
    sheet.write(row, high6_col, c[0].high6)
    sheet.write(row, low7_col, c[0].low7)
    sheet.write(row, high7_col, c[0].high7)
这样,您就可以访问由
zip(cities)
创建的元组的第一个(也是唯一一个)元素


同样,我不明白为什么你不能在城市中使用c语言:,这看起来更好,但这可能会解决你的问题。

我也不明白为什么你需要使用
zip()
,但是如果你确定你需要它,就这样尝试:

for c in zip(cities):
    sheet.write(row, high0_col, c[0].high0)
    sheet.write(row, low1_col, c[0].low1)
    sheet.write(row, high1_col, c[0].high1)
    sheet.write(row, low2_col, c[0].low2)
    sheet.write(row, high2_col, c[0].high2)
    sheet.write(row, low3_col, c[0].low3)
    sheet.write(row, high3_col, c[0].high3)
    sheet.write(row, low4_col, c[0].low4)
    sheet.write(row, high4_col, c[0].high4)
    sheet.write(row, low5_col, c[0].low5)
    sheet.write(row, high5_col, c[0].high5)
    sheet.write(row, low6_col, c[0].low6)
    sheet.write(row, high6_col, c[0].high6)
    sheet.write(row, low7_col, c[0].low7)
    sheet.write(row, high7_col, c[0].high7)
这样,您就可以访问由
zip(cities)
创建的元组的第一个(也是唯一一个)元素



同样,我不明白为什么不能在城市中使用c的
,这看起来更好,但这可能会解决您的问题。

您可以演示如何从canada.getCities()检索元组吗?您在那里一次又一次地做同样的事情,你应该使用一个循环和一个数据结构-你的代码中有一些巨大的反模式。@MatteoD-我已经编辑了上面的代码来说明这一点。Lattyware-我想有一个更简单的方法来做这一切,但我是新手,还不知道所有最好的方法。错误发生在哪一行?您能发布解释器的准确错误通知输出吗?@Asad-我已经在编辑中包含了它。您能展示一下如何从canada.getCities()检索元组吗?您在那里一次又一次地做同样的事情,你应该使用一个循环和一个数据结构-你的代码中有一些巨大的反模式。@MatteoD-我已经编辑了上面的代码来说明这一点。Lattyware-我想有一个更简单的方法来做这一切,但我是新手,还不知道所有最好的方法。错误发生在哪一行?您可以发布解释器的准确错误通知输出吗?@Asad-我已经将其包含在编辑中。我正在调用zip,因为如果我在城市中使用c的
,它将只返回循环中的最后一项(来自canada.py)。这种方法可以成功地在excel文件中输入我需要的所有内容,但它只对列表中的最后一个城市(并非所有34个城市)进行输入。为了向您展示我的意思,我将包含我在原始帖子中导入的所有canada.py脚本。@hunter21188:我仍然不明白您希望
zip
做什么。这意味着不是只获取最后一个值的所有组件,而是只获取每个值的最后一个组件。(如果这真的是你想要的,
zip(*cities)
是答案……但我怀疑这是你想要的。)@hunter21188:另外,当你被这样的事情弄糊涂的时候,试着打印中间值,或者在调试器中单步执行。如果您
print-zip(cities)
(Python 2.x)或
print(list(zip(cities))
(Python 3.x),您应该了解发生了什么。好的,谢谢您的帮助。我关于元组错误的问题已经解决了,但事实证明这不是根本问题。因此,我将针对该问题提出一个新问题。再次感谢。我打电话给zip是因为如果我在城市中使用c的
,它将只返回循环中的最后一项(来自canada.py)。这种方法可以成功地在excel文件中输入我需要的所有内容,但它只对列表中的最后一个城市(并非所有34个城市)进行输入。为了向您展示我的意思,我将包含我在原始帖子中导入的所有canada.py脚本。@hunter21188:我仍然不明白您希望
zip
做什么。这意味着不是只获取最后一个值的所有组件,而是只获取每个值的最后一个组件。(如果这真的是你想要的,
zip(*cities)
是答案……但我怀疑这是你想要的。)@hunter21188:另外,当你被这样的事情弄糊涂的时候,试着打印中间值,或者在调试器中单步执行。如果您
print-zip(cities)
(Python 2.x)或
print(list(zip(cities))
(Python 3.x),您应该了解发生了什么。好的,谢谢您的帮助。我关于元组错误的问题已经解决了,但事实证明这不是根本问题。因此,我将针对该问题提出一个新问题。再次感谢。好的,这确实给了我与城市中c的
完全相同的输出,所以显然我遗漏了一些东西。我需要在excel中输入34列数据。第1列是城市1,第2列是城市2,等等。。。当我在城市中为c使用
,它仅用city34填充第一列。其他一切都是空白的。等一下,现在我知道可能是什么错了:你太过分了
for c in zip(cities):
    sheet.write(row, high0_col, c[0].high0)
    sheet.write(row, low1_col, c[0].low1)
    sheet.write(row, high1_col, c[0].high1)
    sheet.write(row, low2_col, c[0].low2)
    sheet.write(row, high2_col, c[0].high2)
    sheet.write(row, low3_col, c[0].low3)
    sheet.write(row, high3_col, c[0].high3)
    sheet.write(row, low4_col, c[0].low4)
    sheet.write(row, high4_col, c[0].high4)
    sheet.write(row, low5_col, c[0].low5)
    sheet.write(row, high5_col, c[0].high5)
    sheet.write(row, low6_col, c[0].low6)
    sheet.write(row, high6_col, c[0].high6)
    sheet.write(row, low7_col, c[0].low7)
    sheet.write(row, high7_col, c[0].high7)