Search code examples
pythonpython-2.7pokemon-go

Write to file doesnt work "Indentation error"


I tried to save some data to a csv file and i got this error:

i:\Games\Pokemon GO\pokeminer-0.2>python worker.py -st 10
Traceback (most recent call last):
  File "worker.py", line 25, in <module>
    import db
  File "i:\Games\Pokemon GO\pokeminer-0.2\db.py", line 64
    f = open('i:\Games\Pokemon GO\pokeminer-0.2\spawn_location.csv','w')
                                                                       ^
IndentationError: unindent does not match any outer indentation level

And this is the code:

def add_sighting(session, spawn_id, pokemon):  
obj = Sighting(  
    pokemon_id=pokemon['id'],  
    spawn_id=spawn_id,  
    expire_timestamp=pokemon['disappear_time'],  
    normalized_timestamp=normalize_timestamp(pokemon['disappear_time']),  
    lat=pokemon['lat'],  
    lon=pokemon['lng'],  
)  
# Check if there isn't the same entry already  
existing = session.query(Sighting) \  
    .filter(Sighting.pokemon_id == obj.pokemon_id) \  
    .filter(Sighting.spawn_id == obj.spawn_id) \  
    .filter(Sighting.expire_timestamp > obj.expire_timestamp - 10) \  
    .filter(Sighting.expire_timestamp < obj.expire_timestamp + 10) \  
    .filter(Sighting.lat == obj.lat) \  
    .filter(Sighting.lon == obj.lon) \  
    .first()   
if existing:  
    return  
session.add(obj) 

f = open('i:\Games\Pokemon GO\pokeminer-0.2\spawn_location.csv','w')    
f.write(pokemon_id+lon+lat+expire_timestamp)    
f.close()    

This code is not mine, i'm just trying to make it write some details to a csv file. Can you please help me out with this, to make it work?


Solution

  • If the rest of the code is correct, this should do:

    def add_sighting(session, spawn_id, pokemon):  
        obj = Sighting(  
            pokemon_id=pokemon['id'],  
            spawn_id=spawn_id,  
            expire_timestamp=pokemon['disappear_time'],
            normalized_timestamp=normalize_timestamp(pokemon['disappear_time']),  
            lat=pokemon['lat'],  
            lon=pokemon['lng'],  
        )  
        # Check if there isn't the same entry already  
        existing = session.query(Sighting) \  
            .filter(Sighting.pokemon_id == obj.pokemon_id) \  
            .filter(Sighting.spawn_id == obj.spawn_id) \  
            .filter(Sighting.expire_timestamp > obj.expire_timestamp - 10)     \
            .filter(Sighting.expire_timestamp < obj.expire_timestamp + 10) \ 
            .filter(Sighting.lat == obj.lat) \  
            .filter(Sighting.lon == obj.lon) \  
            .first()   
        if existing:  
            return  
        session.add(obj) 
    
        f = open('i:\Games\Pokemon GO\pokeminer-0.2\spawn_location.csv','w')    
        f.write(pokemon_id+lon+lat+expire_timestamp)    
        f.close()
    

    That's because Python does not use explicit delimiters to its functions like other languages (e.g. C flavored languages, which use curly braces {}). Instead, Python uses indentation to define where functions start and where they end. def is how you start a function, and add_sighting is the name of the function. Everything after that is (probably) within the function, and therefore should be indented.