Python 写入文件无效“缩进错误”

Python 写入文件无效“缩进错误”,python,python-2.7,pokemon-go,Python,Python 2.7,Pokemon Go,我试图将一些数据保存到csv文件,但出现以下错误: 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 = o

我试图将一些数据保存到csv文件,但出现以下错误:

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
这是代码:

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()    

这段代码不是我的,我只是想让它写一些细节到一个csv文件。你能帮我解决这个问题吗?如果代码的其余部分是正确的,这应该可以:

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()

这是因为Python不像其他语言那样对其函数使用显式分隔符,例如使用大括号{}的C风格语言。相反,Python使用缩进定义函数的开始和结束位置。def是启动函数的方式,add_sighting是函数的名称。之后的所有内容可能都在函数中,因此应该缩进。

函数中def语句之后的代码应该缩进。您展示的代码在Python中永远不会工作。回到你的密码来源。