Python Forum
Using a variable to replace an integer? (Except it isn't working!)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a variable to replace an integer? (Except it isn't working!)
#11
Attempt:

#note: the url is actually there in the code, but it was preventing me from posting this
url = 'freegeoip'
try:
     with closing(urlopen(url)) as response:
           location = json.loads(response.read())
           loc_c = location['city']
           loc_s = location['region_name']
except:
           print('step 1 failed')
 
client = yweather.Client()
wid = client.fetch_woeid('loc_c, loc_s')
print(wid)
print('steps 1 and 2 finished')
 
print('step 3 started')
loc_weather = client.fetch_weather(wid)
loc_weather["condition"]["text"]
loc_weather["condition"]["temp"]
print('step 3 finished')
Also while I was typing this, a new traceback popped up:
Error:
Traceback (most recent call last): File "weather.py", line 21, in <module> loc_weather = client.fetch_weather(wid) File "/usr/local/lib/python2.7/dist-packages/yweather.py", line 180, in fetch_weather rss = self._fetch_xml(url) File "/usr/local/lib/python2.7/dist-packages/yweather.py", line 344, in _fetch_xml with contextlib.closing(urlopen(url)) as f: File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 429, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 447, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open raise URLError(err) urllib2.URLError: <urlopen error [Errno 22] Invalid argument>
Reply
#12
As I said you need to change also line 12

wid = client.fetch_woeid('loc_c, loc_s')
should be

wid = client.fetch_woeid("{}, {}".format(loc_c, loc_s))
Reply
#13
Tried that as well. New traceback:
Error:
Traceback (most recent call last):   File "weather.py", line 21, in <module>     loc_weather = client.fetch_weather(wid)   File "/usr/local/lib/python2.7/dist-packages/yweather.py", line 180, in fetch_weather     rss = self._fetch_xml(url)   File "/usr/local/lib/python2.7/dist-packages/yweather.py", line 344, in _fetch_xml     with contextlib.closing(urlopen(url)) as f:   File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen     return opener.open(url, data, timeout)   File "/usr/lib/python2.7/urllib2.py", line 429, in open     response = self._open(req, data)   File "/usr/lib/python2.7/urllib2.py", line 447, in _open     '_open', req)   File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain     result = func(*args)   File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open     return self.do_open(httplib.HTTPConnection, req)   File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open     raise URLError(err) urllib2.URLError: <urlopen error [Errno 110] Connection timed out>
Reply
#14
always post the code, the output and like you did - the full traceback.

what was the output from
print (wid)
Reply
#15
well, now I went to github repo of yweather package
it saysYahoo Weather has discontinued the RSS feed interface this library is based on. There is a new YQL Query interface: https://developer.yahoo.com/weather/

I don't plan on updating this library to use the new endpoint.

any reason why you expect it to work?
Reply
#16
(Jul-25-2017, 06:39 PM)buran Wrote: always post the code, the output and like you did - the full traceback.

what was the output from
print (wid)

The print(wid) gave me "2449574" which is the correct woe_id for where I live, so that part definitely works! (At least I got the first part right, haha)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 680 Nov-28-2023, 11:36 PM
Last Post: chitarup
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,492 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,258 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Why replace treats an integer value 999 as 999.0? new_to_python 10 4,237 Feb-16-2020, 01:49 PM
Last Post: new_to_python
  Mixed string,Integer input variable issue maderdash 2 2,714 Nov-06-2018, 09:46 AM
Last Post: snippsat
  replace html string with python variable ! evilcode1 4 6,067 Aug-30-2018, 03:14 PM
Last Post: snippsat
  Search & Replace - Newlines Added After Replace dj99 3 3,356 Jul-22-2018, 01:42 PM
Last Post: buran
  string with integer variable within Andy1845c 9 4,854 Mar-12-2018, 10:28 PM
Last Post: Larz60+
  int(variable) not changing variable to an integer StoopidChicken 2 2,901 Jan-11-2018, 10:30 AM
Last Post: StoopidChicken

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020