我知道在Python 3.6中引入了F字符串。由此我遇到了错误:"语法无效"
DATA_FILENAME = 'data.json'
def load_data(apps, schema_editor):
Shop = apps.get_model('shops', 'Shop')
jsonfile = Path(__file__).parents[2] / DATA_FILENAME
with open(str(jsonfile)) as datafile:
objects = json.load(datafile)
for obj in objects['elements']:
try:
objType = obj['type']
if objType == 'node':
tags = obj['tags']
name = tags.get('name','no-name')
longitude = obj.get('lon', 0)
latitude = obj.get('lat', 0)
location = fromstr(F'POINT({longitude} {latitude})', srid=4326)
Shop(name=name, location = location).save()
except KeyError:
pass
错误:
location = (F'POINT({longitude} {latitude})', srid=4326)
^
SyntaxError: invalid syntax
因此,我使用了:
fromstr('POINT({} {})'.format(longitude, latitude), srid=4326)
错误已被除去并且对我起作用。然后我发现这个库future-fstrings。我应该使用它吗?这将消除上述的"Invalid Error"吗?