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

  • SEARCH
  • Home
  • 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 7918367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:28:55+00:00 2026-06-03T15:28:55+00:00

Greetings fellow enthusiasts: I am debugging a program integrated with the twitter-api where the

  • 0

Greetings fellow enthusiasts:

I am debugging a program integrated with the twitter-api where the purpose is to search for specific tweets in a given radius and returns them in a csv file.

It works….yet sometimes it encounters a ” KeyError: ‘location’ ” error yet there is no key error when I check it so can anyone shed any light as to why it’s returning this error?

If you uncomment out the pprint statement to see the raw returned data from twitter-api it is clear ‘location’ is the correct key and works sometimes but sometimes not and returns the aforementioned error.

Below is the code run it with the test input given in the comments:

import urllib2, json, pprint, codecs, unicodedata, csv
## test coordinate input: 29.762778,-95.383056
## test radius 10
## test query: tebow
##Initial user input
city = raw_input("Please enter to 6 decimal places the city\ncoordinates to be searched ex. lat,long: ")
radius = raw_input("Please enter the numeric value of the\nradius in miles you'd like to search ex. 10: ")
term= raw_input("Please enter the search term you wish to query ex. tebow: ")

u = urllib2.urlopen('http://search.twitter.com/search.json?q='+term+'&geocode='+city+','+radius+'mi&page=1&rpp=20')
datares = json.load(u)
##pprint.pprint(datares)

with codecs.open('Gtweets.csv',mode='w', encoding='ascii',errors='ignore') as cache:
    writer = csv.writer(cache)
    for tweet in datares['results']:
        writer.writerow([tweet['text'].encode('ascii', 'ignore'), tweet['location'].encode('ascii', 'ignore'), tweet['created_at'].encode('ascii', 'ignore'), tweet['from_user'].encode('ascii', 'ignore')])
  • 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-03T15:28:59+00:00Added an answer on June 3, 2026 at 3:28 pm

    Since you don’t have any control over whether Twitter includes the location or not, don’t rely on the field being present. Instead of using square brackets for the lookup, use dict.get instead:

    tweet.get('location', '<unknown location>').encode('ascii', 'ignore')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings fellow enthusiasts: I am debugging a program integrated with the twitter-api where the
Greetings fellow enthusiasts! I am working on a project and I am utilizing python
Greetings, fellow StackOverFlow members. I have just begun to learn how to program web-enabled
Greetings fellow members, the situation in question is such: public abstract class BaseClass {
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
Greetings fellow earthlings. I'm researching methods of caching data retrieved from various database tables
Greetings fellow Droidheads and the like. I am working on a Live Wallpaper project
Greetings, I am the program writer for the rebellion against the evil galactic empire.
Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
Greetings fellow amazonians We have certain f4v files from webtvinteractive.com that are not working

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.