我为什么收到" ValueError"这个节目的消息?

时间:2017-03-12 23:40:07

标签: python matplotlib

编辑(2017年3月3日[9:11 CST]) - 所以我能够确定数字为235-235的原因是字符串中指定要进入数组的字符总数,温度至少一个。所以现在,我的问题是找到它为什么计算字符而不是字符串的总数。

我正在编写一个程序,从Weather Underground中提取实时数据,将其分类到数组中,然后最终输出完整的图形和文本报告。

目前,我无法将数据转换为可用于使用matplotlib模块制作图形的格式。完整的代码及其模块"计算"在这篇文章的底部。

错误消息如下:

Traceback (most recent call last):
  File "C:/Users/Ryan/PycharmProjects/NWS/weather_data.py", line 254, in <module>
    temperatures_array = np.array(temp).reshape(10, 2)
ValueError: cannot reshape array of size 235 into shape (10,2)

我不明白为什么我收到这个错误,因为数组的大小应该是20,而不是235.如果有人能指出我做错了什么,以及我如何从这里开始,我会非常感激它

重要提示::除非您生成自己的Weather Underground API密钥,否则请勿尝试运行此操作。没有它,它将无法运作。在https://www.wunderground.com/weather/api/

获取密钥

代码:

import numpy as np
from calculations import *
import time as t
import matplotlib.pyplot as plt

plt.style.use("classic")


Juneau = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None, "winddirection": None,
          "pressure": None, "humidity": None}

Denver = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None, "winddirection": None,
          "pressure": None, "humidity": None}

Atlanta = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None, "winddirection": None,
           "pressure": None, "humidity": None}

Honolulu = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None, "winddirection": None,
            "pressure": None, "humidity": None}

Springfield = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None,
               "winddirection": None, "pressure": None, "humidity": None}

OklahomaCity = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None,
                "winddirection": None, "pressure": None, "humidity": None}

Austin = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None, "winddirection": None,
          "pressure": None, "humidity": None}

Montpelier = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None,
              "winddirection": None, "pressure": None, "humidity": None}

Richmond = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None, "winddirection": None,
            "pressure": None, "humidity": None}

Olympia = {"temperature": None, "feelslike": None, "windspeed": None, "windgustspeed": None, "winddirection": None,
           "pressure": None, "humidity": None}

J = Juneau
D = Denver
AT = Atlanta
H = Honolulu
S = Springfield
OC = OklahomaCity
AU = Austin
M = Montpelier
R = Richmond
OL = Olympia

# Data Setting for Juneau
v1, v2, v3, v4, v5, v6, v7 = get_data("Alaska", "Juneau")
J["temperature"] = v1
J["feelslike"] = v2
J["windspeed"] = v3
J["windgustspeed"] = v4
J["winddirection"] = v5
J["pressure"] = v6
J["humidity"] = v7
print("\nJuneau Data:", J)

# Data Setting for Denver
v1, v2, v3, v4, v5, v6, v7 = get_data("Colorado", "Denver")
D["temperature"] = v1
D["feelslike"] = v2
D["windspeed"] = v3
D["windgustspeed"] = v4
D["winddirection"] = v5
D["pressure"] = v6
D["humidity"] = v7
print("\nDenver Data:", D)

# Data Setting for Atlanta
v1, v2, v3, v4, v5, v6, v7 = get_data("Georgia", "Atlanta")
AT["temperature"] = v1
AT["feelslike"] = v2
AT["windspeed"] = v3
AT["windgustspeed"] = v4
AT["winddirection"] = v5
AT["pressure"] = v6
AT["humidity"] = v7
print("\nAtlanta Data:", AT)

# Data Setting for Honolulu
v1, v2, v3, v4, v5, v6, v7 = get_data("Hawaii", "Honolulu")
H["temperature"] = v1
H["feelslike"] = v2
H["windspeed"] = v3
H["windgustspeed"] = v4
H["winddirection"] = v5
H["pressure"] = v6
H["humidity"] = v7
print("\nHonolulu Data:", H)

# Data Setting for Springfield
v1, v2, v3, v4, v5, v6, v7 = get_data("Illinois", "Springfield")
S["temperature"] = v1
S["feelslike"] = v2
S["windspeed"] = v3
S["windgustspeed"] = v4
S["winddirection"] = v5
S["pressure"] = v6
S["humidity"] = v7
print("\nSpringfield Data:", S)

# Data Setting for Oklahoma City
v1, v2, v3, v4, v5, v6, v7 = get_data("Oklahoma", "Oklahoma_City")
OC["temperature"] = v1
OC["feelslike"] = v2
OC["windspeed"] = v3
OC["windgustspeed"] = v4
OC["winddirection"] = v5
OC["pressure"] = v6
OC["humidity"] = v7
print("\nOklahoma City Data:", OC)

# Data Setting for Austin
v1, v2, v3, v4, v5, v6, v7 = get_data("Texas", "Austin")
AU["temperature"] = v1
AU["feelslike"] = v2
AU["windspeed"] = v3
AU["windgustspeed"] = v4
AU["winddirection"] = v5
AU["pressure"] = v6
AU["humidity"] = v7
print("\nAustin Data:", AU)

# Data Setting for Montpelier
v1, v2, v3, v4, v5, v6, v7 = get_data("Vermont", "Montpelier")
M["temperature"] = v1
M["feelslike"] = v2
M["windspeed"] = v3
M["windgustspeed"] = v4
M["winddirection"] = v5
M["pressure"] = v6
M["humidity"] = v7
print("\nMontpelier Data:", M)

# Data Setting for Richmond
v1, v2, v3, v4, v5, v6, v7 = get_data("Virginia", "Richmond")
R["temperature"] = v1
R["feelslike"] = v2
R["windspeed"] = v3
R["windgustspeed"] = v4
R["winddirection"] = v5
R["pressure"] = v6
R["humidity"] = v7
print("\nRichmond Data:", R)

# Data Setting for Olympia
v1, v2, v3, v4, v5, v6, v7 = get_data("Washington", "Olympia")
OL["temperature"] = v1
OL["feelslike"] = v2
OL["windspeed"] = v3
OL["windgustspeed"] = v4
OL["winddirection"] = v5
OL["pressure"] = v6
OL["humidity"] = v7
print("\nOlympia Data:", OL)

temperatures = []
temperatures.extend([str(AU["temperature"]) + " F"])
temperatures.extend([str(H["temperature"]) + " F"])
temperatures.extend([str(M["temperature"]) + " F"])
temperatures.extend([str(R["temperature"]) + " F"])
temperatures.extend([str(OL["temperature"]) + " F"])
temperatures.extend([str(J["temperature"]) + " F"])
temperatures.extend([str(AT["temperature"]) + " F"])
temperatures.extend([str(S["temperature"]) + " F"])
temperatures.extend([str(D["temperature"]) + " F"])
temperatures.extend([str(OC["temperature"]) + " F"])

feelslike = []
feelslike.extend([str(AU["feelslike"]) + " F"])
feelslike.extend([str(H["feelslike"]) + " F"])
feelslike.extend([str(M["feelslike"]) + " F"])
feelslike.extend([str(R["feelslike"]) + " F"])
feelslike.extend([str(OL["feelslike"]) + " F"])
feelslike.extend([str(J["feelslike"]) + " F"])
feelslike.extend([str(AT["feelslike"]) + " F"])
feelslike.extend([str(S["feelslike"]) + " F"])
feelslike.extend([str(D["feelslike"]) + " F"])
feelslike.extend([str(OC["feelslike"]) + " F"])

windspeed = []
windspeed.extend([str(AU["windspeed"]) + " MPH"])
windspeed.extend([str(H["windspeed"]) + " MPH"])
windspeed.extend([str(M["windspeed"]) + " MPH"])
windspeed.extend([str(R["windspeed"]) + " MPH"])
windspeed.extend([str(OL["windspeed"]) + " MPH"])
windspeed.extend([str(J["windspeed"]) + " MPH"])
windspeed.extend([str(AT["windspeed"]) + " MPH"])
windspeed.extend([str(S["windspeed"]) + " MPH"])
windspeed.extend([str(D["windspeed"]) + " MPH"])
windspeed.extend([str(OC["windspeed"]) + " MPH"])

windgustspeed = []
windgustspeed.extend([str(AU["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(H["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(M["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(R["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(OL["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(J["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(AT["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(S["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(D["windgustspeed"]) + " MPH"])
windgustspeed.extend([str(OC["windgustspeed"]) + " MPH"])

winddirection = []
winddirection.extend([AU["winddirection"]])
winddirection.extend([H["winddirection"]])
winddirection.extend([M["winddirection"]])
winddirection.extend([R["winddirection"]])
winddirection.extend([OL["winddirection"]])
winddirection.extend([J["winddirection"]])
winddirection.extend([AT["winddirection"]])
winddirection.extend([S["winddirection"]])
winddirection.extend([D["winddirection"]])
winddirection.extend([OC["winddirection"]])

pressure = []
pressure.extend([str(AU["pressure"]) + " in"])
pressure.extend([str(H["pressure"]) + " in"])
pressure.extend([str(M["pressure"]) + " in"])
pressure.extend([str(R["pressure"]) + " in"])
pressure.extend([str(OL["pressure"]) + " in"])
pressure.extend([str(J["pressure"]) + " in"])
pressure.extend([str(AT["pressure"]) + " in"])
pressure.extend([str(S["pressure"]) + " in"])
pressure.extend([str(D["pressure"]) + " in"])
pressure.extend([str(OC["pressure"]) + " in"])

humidity = []
humidity.extend([AU["humidity"]])
humidity.extend([H["humidity"]])
humidity.extend([M["humidity"]])
humidity.extend([R["humidity"]])
humidity.extend([OL["humidity"]])
humidity.extend([J["humidity"]])
humidity.extend([AT["humidity"]])
humidity.extend([S["humidity"]])
humidity.extend([D["humidity"]])
humidity.extend([OC["humidity"]])

areas = ["Austin, Texas", "Honolulu, Hawaii", "Montpelier, Vermont", "Richmond, Virginia", "Olympia, Washington", "Juneau, Alaska", "Atlanta, Georgia", "Springfield, Illinois", "Denver, Colorado", "Oklahoma City, Oklahoma"]

temp = []
x = 10
y = 0
while x > 0:
    temp.extend(areas[y])
    temp.extend(temperatures[y])
    y += 1
    x -= 1
temperatures_array = np.array(temp).reshape(10, 2)
print("\nTemperatures:")
print(temperatures_array)

temp = []
x = 10
y = 0
while x > 0:
    temp.extend(areas[y])
    temp.extend(feelslike[y])
    y += 1
    x -= 1
feelslike_array = np.array(temp).reshape(10, 2)
print("\nFeels Like Temperatures:")
print(feelslike_array)

temp = []
x = 10
y = 0
while x > 0:
    temp.extend(areas[y])
    temp.extend(windspeed[y])
    y += 1
    x -= 1
windspeed_array = np.array(temp).reshape(10, 2)
print("\nWind Speeds:")
print(windspeed_array)

temp = []
x = 10
y = 0
while x > 0:
    temp.extend(areas[y])
    temp.extend(windgustspeed[y])
    y += 1
    x -= 1
windgustspeed_array = np.array(temp).reshape(10, 2)
print("\nWind Gust Speeds:")
print(windgustspeed_array)

temp = []
x = 10
y = 0
while x > 0:
    temp.extend(areas[y])
    temp.extend(winddirection[y])
    y += 1
    x -= 1
winddirection_array = np.array(temp).reshape(10, 2)
print("\nWind Directions:")
print(winddirection_array)

temp = []
x = 10
y = 0
while x > 0:
    temp.extend(areas[y])
    temp.extend(pressure[y])
    y += 1
    x -= 1
pressure_array = np.array(temp).reshape(10, 2)
print("\nPressures:")
print(pressure_array)

temp = []
x = 10
y = 0
while x > 0:
    temp.extend(areas[y])
    temp.extend(humidity[y])
    y += 1
    x -= 1
humidity_array = np.array(temp).reshape(10, 2)
print("\nHumidities:")
print(humidity_array)

time = []
time.extend(t.gmtime())
filename = ("Report" + "_" + str(time[0]) + "_" + str(time[1]) + "_" + str(time[2]) + "_" + str(time[3]) + "_" + str(time[4]) + "_" + str(time[5]))

openfile = open(filename, "a")

openfile.write("Temperatures:")
openfile.write("\n")
openfile.write(str(temperatures_array))
openfile.write("\n")
openfile.write("\nFeels Like Temperatures:")
openfile.write("\n")
openfile.write(str(feelslike_array))
openfile.write("\n")
openfile.write("\nWind Speeds:")
openfile.write("\n")
openfile.write(str(windspeed_array))
openfile.write("\n")
openfile.write("\nWind Gust Speeds:")
openfile.write("\n")
openfile.write(str(windgustspeed_array))
openfile.write("\n")
openfile.write("\nWind Directions:")
openfile.write("\n")
openfile.write(str(winddirection_array))
openfile.write("\n")
openfile.write("\nPressures:")
openfile.write("\n")
openfile.write(str(pressure_array))
openfile.write("\n")
openfile.write("\nHumidities:")
openfile.write("\n")
openfile.write(str(humidity_array))

openfile.close()

figure = plt.hist(temperatures_array, color="steelblue")
figure.savefig("temperature_graph.png")

figure = plt.hist(feelslike_array, color="steelblue")
figure.savefig("temperature_graph.png")

figure = plt.hist(windspeed_array, color="steelblue")
figure.savefig("temperature_graph.png")

figure = plt.hist(windgustspeed_array, color="steelblue")
figure.savefig("temperature_graph.png")

figure = plt.hist(pressure_array, color="steelblue")
figure.savefig("temperature_graph.png")

figure = plt.hist(humidity_array, color="steelblue")
figure.savefig("temperature_graph.png")

input("\nPress enter to exit.")

模块&#34;计算&#34;:

import urllib3
import json


def get_data(state, city):
    wukey = [REDACTED FOR SAFETY]
    url = "http://api.wunderground.com/api/{}/conditions/forecast/q/{}/{}.json".format(wukey, state, city)
    http = urllib3.PoolManager()

    response = http.request('GET', url)
    json_string = response.data.decode('utf8')
    parsed_json = json.loads(json_string)
    temp = float(parsed_json['current_observation']['temp_f'])
    feelslike = float(parsed_json['current_observation']['feelslike_f'])
    windspeed = float(parsed_json['current_observation']['wind_mph'])
    windgust = float(parsed_json['current_observation']['wind_gust_mph'])
    winddir = str(parsed_json['current_observation']['wind_dir'])
    pressure = float(parsed_json['current_observation']['pressure_in'])
    humidity = str(parsed_json['current_observation']['relative_humidity'])

    response.close()
    return temp, feelslike, windspeed, windgust, winddir, pressure, humidity

提前感谢任何帮助过的人!

1 个答案:

答案 0 :(得分:1)

问题似乎是您在while循环中使用extend而不是append

我还没有运行您的所有代码,但我确定您的问题来自您的while循环:

while x > 0:
    temp.extend(areas[y])
    temp.extend(temperatures[y])
    y += 1
    x -= 1

如果你在while循环的每次迭代中打印出temp的样子,你会发现extend 将每个字符串分成一个字符列表(如{{ 1}}意味着工作 - 将一个列表合并到另一个列表中:

extend

你的代码应该是这样的循环:

['A', 'u', 's', 't', 'i', 'n', ',', ' ', 'T', 'e', 'x', 'a', 's', '1', '2', '3', '4', ' ', 'F']

['A', 'u', 's', 't', 'i', 'n', ',', ' ', 'T', 'e', 'x', 'a', 's', '1', '2', '3', '4', ' ', 'F', 'H', 'o', 'n', 'o', 'l', 'u', 'l', 'u', ',', ' ', 'H', 'a', 'w', 'a', 'i', 'i', '1', '2', '3', '4', ' ', 'F']

... and so on, until you have 235 elements.

Ideone再次为我工作。这是我查看结果的代码段:https://ideone.com/zGqxiK

小记:

使用for循环而不是while循环可以让事情变得更容易:

while x > 0:
    temp.append(areas[y])
    temp.append(temperatures[y])
    y += 1
    x -= 1

您可以对代码进行一些细微的优化,虽然我不确定在这里注意它们是否偏离原始帖子(或者应该保留给另一个StackExchange站点或帖子)。