Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F String Invalid Syntax in Python 3.5 [closed]

I know that F Strings were introduce in Python 3.6. For that i was getting error - Invalid Syntax

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    

Error -

location = (F'POINT({longitude} {latitude})', srid=4326)
                                           ^
SyntaxError: invalid syntax

So i used -

fromstr('POINT({} {})'.format(longitude, latitude), srid=4326)

The Error was removed and it worked for me. Then i found this library future-fstrings. Should i use it. Which will remove the above Invalid Error

like image 582
Cipher Avatar asked Mar 15 '19 12:03

Cipher


People also ask

How do I fix the syntax error when using F-strings?

To fix this issue, change the python interpreter from 2 to 3. f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error. If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.

How to fix SyntaxError invalid syntax in Python?

The message reads SyntaxError: invalid syntax, but that’s not very helpful. The traceback points to the first place where Python could detect that something was wrong. To fix this sort of error, make sure that all of your Python keywords are spelled correctly. Another common issue with keywords is when you miss them altogether:

Is it necessary to use F-strings in Python 3?

And it's not necessary in the way that the print function change was. You can write code that's compatible with 3.5 and 3.6 by just not using f-strings. That's not the case with the print function vs print statement. It's impossible (*) to write code that works under both without the help of __future__.

Why is my Python interpreter not working with F strings?

Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3.


1 Answers

For older versions of Python (before 3.6):

Using future-fstrings:

pip install future-fstrings 

you have to place a special line at the top of your code:

coding: future_fstrings

Hence in your case:

# -*- coding: future_fstrings -*-
# rest of the code
location = fromstr(f'POINT({longitude} {latitude})', srid=4326)
like image 75
DirtyBit Avatar answered Sep 26 '22 23:09

DirtyBit