Patrick Sanchez Patrick Sanchez - 1 month ago 10
Python Question

Python Indention Block? Why?

I have tried untabifying region.. and did not mix spaces/tabs.. What could be wrong here? When I run the module it traces to

if result["geo"]:
and says "There's an error in your program: expected an indention block"

from twitter import *

import sys
import csv

latitude = 8.8015
longitude = 125.7407
max_range = 1000
num_results = 500
outfile = "nimal.csv"

config = {}
execfile("config.py", config)
twitter = Twitter(
auth = OAuth(config["access_key"], config["access_secret"], config["consumer_key"], config["consumer_secret"]))

csvfile = file(outfile, "w")
csvwriter = csv.writer(csvfile)
row = [ "date", "user", "text", "latitude", "longitude" ]
csvwriter.writerow(row)

result_count = 0
last_id = None
while result_count < num_results:
query = twitter.search.tweets(q = "urios", geocode = "%f,%f,%dkm" % (latitude, longitude, max_range), count = 100, since_id = 2016-10-8, max_id = last_id)

for result in query["statuses"]:
if result["geo"]:
date = result["created_at"]
user = result["user"]["screen_name"]
text = text.encode('ascii', 'replace')
latitude = result["geo"]["coordinates"][0]
longitude = result["geo"]["coordinates"][1]
row = [ date, user, text, latitude, longitude ]
csvwriter.writerow(row)
result_count += 1
last_id = result["id"]

print "got %d results" % result_count


csvfile.close()

print "written to %s" % outfile

Answer

here is the problem :

for result in query["statuses"]:
if result["geo"]:
            date = result["created_at"]

python has specific syntax and it has to be considered
you have to change it to:

for result in query["statuses"]:
    if result["geo"]:
            date = result["created_at"]