我试图将一些数据保存到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文件。你可以帮我解决一下这个问题吗?
答案 0 :(得分:0)
如果其余代码正确,则应执行以下操作:
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
是函数的名称。之后的所有内容(可能)都在函数内,因此应该缩进。