Python 3.5中的F字符串无效语法
我知道Python 3.5中的F字符串无效语法,python,python-3.x,Python,Python 3.x,我知道F字符串是在python3.6中引入的。因此,我得到了错误-无效语法 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:
F字符串
是在python3.6
中引入的。因此,我得到了错误-无效语法
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)
这个错误被删除了,它对我有效。然后我找到了这个图书馆。我应该用它吗。对于较早版本的Python(3.6之前的版本),这将删除上面的无效错误: 使用: 您必须在代码顶部放置一个特殊的行:
coding: future_fstrings
因此,在你的情况下:
# -*- coding: future_fstrings -*-
# rest of the code
location = fromstr(f'POINT({longitude} {latitude})', srid=4326)
格式(“f”)字符串在Python 3.6之前还没有引入。好吧,f字符串仅在Python 3.6之后才可用。为什么错误消息与您的代码不匹配?可能只是一个复制粘贴错误。很可能是Bytestring quantum tunnelling;-)。仅使用最仁慈的解释。语句必须从散列标记开始,即“#--coding:future_fstrings--”,否则它将无法工作。请更正你的答案。
# -*- coding: future_fstrings -*-
# rest of the code
location = fromstr(f'POINT({longitude} {latitude})', srid=4326)