写入文件不起作用 "Indentation error"
Write to file doesnt work "Indentation error"
我试图将一些数据保存到 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
是函数的名称。之后的所有内容(可能)都在函数中,因此应该缩进。
我试图将一些数据保存到 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
是函数的名称。之后的所有内容(可能)都在函数中,因此应该缩进。