Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:14:41+00:00 2026-06-15T16:14:41+00:00

I found and modified simple code to get weather condition in Python using openweather

  • 0

I found and modified simple code to get weather condition in Python using openweather and json format. But I have a problem – how can I say that the city is incorrect?

I mean even if I pass a wrong, nonexisting city, read always gives an answer (theres no such thing like ’empty response’ or something like that).

See the code below to see what I’m talking about:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2, json

city = "etre4t5r5e4re" # the city name is incorrent
url = "http://openweathermap.org/data/2.1/forecast/city?q="
url += city
try :
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
    info = wx.MessageBox(u"Internet connection error", u"Error", wx.OK | wx.ICON_ERROR)
except urllib2.URLError, e:
    info = wx.MessageBox(u"Internet connection error", u"Error", wx.OK | wx.ICON_ERROR)
except httplib.HTTPException, e:
    info = wx.MessageBox(u"Internet connection error", u"Error", wx.OK | wx.ICON_ERROR)
except Exception:
    info = wx.MessageBox(u"Error", u"Error", wx.OK | wx.ICON_ERROR)
weather = response.read()

if __name__ == '__main__':
    print(weather) # it will show weather but thats not what I want for non-existing city!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:14:42+00:00Added an answer on June 15, 2026 at 4:14 pm

    There is a recurrent ID when the city requested doesn’t exist, you can maybe based your code on that, or make a second request. I’ve explained the two solutions that I would have used.

    #!/usr/bin/python
    
    import urllib2, json
    
    city = "etre4t5r5e4re"
    root = "http://openweathermap.org/data/2.1/forecast/city?q=%s"
    url  = root % city
    
    response = urllib2.urlopen(url)
    j = json.load(response)
    
    # Solution 1
    if j.get('url', '').split('/')[-1] == '7284885':
        print " ! This city seems to be THE Unknown city"
    
    # Solution 2
    if 'No station' in urllib2.urlopen(j.get('url')).read():
        print " ! Again.. This city seems to be THE Unknown city"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've set up a simple camera app using code I found online. I have
//UPDATE// I found some code to do paging which I have modified to work
I found this code in the internet and I modified it for my use,
FOUND THE ANSWER MYSELF, see below... I'd like to have a very simple user
found this little code snippet that seems to do what i want, but im
Every sample code I've ever found for drawing rounded rectangles using GDI+ goes something
I have some Java code that validates XML against an XSD. I am using
I found some code on the internet as below (slightly modified). It simply requests
I'm using Grails 1.3.7. I have some code that uses the built-in base64Encode function
Found this very simple code to show character counts for my text inputs: http://www.jamesfairhurst.co.uk/posts/view/jquery_word_character_counter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.