【问题标题】:Python Wunderground raw_input dataPython Wunderground raw_input 数据
【发布时间】:2016-10-26 15:27:22
【问题描述】:

我正在尝试让程序询问用户机场代码和年份(可以是任何东西 - 它不一定是正确的或任何具体的。我的教授只是希望它询问然后打印数据。 ) 这是我的代码:

import urllib2
from bs4 import BeautifulSoup

# Create / open a file called wunderdata.txt which will be a CSVfile
f = open('wunderdata.txt', 'w')

# Iterate through months and day
for m in range(1, 13):
    for d in range(1,32):
        # Check if already processed all days in the month
        if (m == 2 and d> 28):
            break
        elif (m in[4, 6, 9, 11] and d > 30):
            break

    # Open wunderground.com url
    airport = str(raw_input("Enter airport code: "))
    year = str(raw_input("Enter year: "))

    timestamp = '2009' + str(m) + str(d)
    print ("Getting data for ") + timestamp

    url = "http://www.wunderground.com/history/airport/" + airport + "/" + year + "/" + str(m) + "/" + str(d) + "/DailyHistory.html?"
    page = urllib2.urlopen(url)     

    # Get temperature from page
    soup = BeautifulSoup(page, "html.parser")


    #the following two lines are the original (textbook) and first attempt to fix
    # dayTemp = soup.body.wx-value.b.string
    dayTemp = soup.findAll(attrs={"class":"wx-value"})[6].get_text()
    seaLevel = soup.findAll(attrs={"class":"wx-value"})[16].get_text()      



    # Format month for timestamp
    if len(str(m)) < 2:
        mStamp = '0' + str(m)
    else:
        mStamp = str(m)

    # Format day for timestamp
    if len(str(d)) < 2:
        dStamp = '0' + str(d)
    else:
        dStamp = str(d)

    # Build timestamp
    #timestamp = '2009' + mStamp + dStamp

    # Write timestamp and temperature to file
    f.write(timestamp + ',' + dayTemp + " " + "Sea Level Pressure: " + seaLevel + '\n')

# Done getting data! Close file.
f.close()

无论如何,这是输入时出现的:

python get-weather-data.py
Enter airport code: KBUF
Enter year: 2009
Getting data for 200911
Enter airport code: KBUF
Enter year: 2009
Getting data for 200912
Enter airport code: KBUF
Enter year: 2009
Getting data for 200913

我希望它是

python get-weather-data.py
Enter airport code: KBUF
Enter year: 2009
Getting data for 200911
Getting data for 200912
Getting data for 200913

有人帮忙!我是初学者,所以我对python不太了解,但非常感谢帮助:)

【问题讨论】:

    标签: python loops input raw-input


    【解决方案1】:

    问题是你要求输入inside循环。因此,您每次通过该代码时都要求输入。

    如果您只想获取一次输入,请将其放在循环之外。考虑:

    airport = str(raw_input("Enter airport code: "))
    year = str(raw_input("Enter year: "))
    
    for m in range(1, 13):
        for d in range(1,32):
            # Check if already processed all days in the month
    ...
    

    【讨论】:

      【解决方案2】:

      您的问题标题有点误导,因为普通用户不知道“wunderdata”是什么。而你的问题也与此无关。

      根据我对您的问题的了解,就像将您的 raw_input 语句放在 for 循环之外一样简单:

      # Create / open a file called wunderdata.txt which will be a CSVfile
      f = open('wunderdata.txt', 'w')
      
      # Enter necessary data
      airport = str(raw_input("Enter airport code: "))
      year = str(raw_input("Enter year: "))
      
      # Iterate through months and day
      for m in range(1, 13):
          ...
      

      我相信raw_input 已经返回了一个字符串,因此不需要转换,但是,我不完全确定这一点,因为我使用的是 python 3.x

      【讨论】:

        猜你喜欢
        • 2015-12-03
        • 1970-01-01
        • 2017-12-08
        • 1970-01-01
        • 2011-04-06
        • 1970-01-01
        • 1970-01-01
        • 2017-02-15
        • 1970-01-01
        相关资源
        最近更新 更多