简体   繁体   中英

Python3 giving syntax error for some reason for single quote

Hi I have a Python3 script that exports a CSV file to a MYSQL RDS Server. Below is the line of the code that gives me syntax error due some reason I am not able to debug it at all. Python Version Python 3.5.3

def connect_to_db():
    connection_string = f'mysql+pymysql://{USR}:{PWD}@{ENDPOINT}:{PORT}/{DBNAME}'
    logger.debug(f'Create DB Connection with string - {connection_string}')
    try:
        engine = create_engine(connection_string)
        logger.info("Connected successfully")
        return engine
    except Exception as e:
        logger.error(e)
        return None

Below image shows the error. I tried both combination single and double quote same error.

在此处输入图片说明

f strings are introduced from Python 3.6 onwards. If you want to use the f string syntax in your current python version you can use future-fstrings which is a backport of fstrings to python<3.6.

Alternatively you can also use the advanced string formatting

>>> 'mysql+pymysql://{USR}:{PWD}@{ENDPOINT}:{PORT}/{DBNAME}'.format(USR="testuser", PWD="password", ENDPOINT="endpoint", PORT=123, DBNAME="name")
'mysql+pymysql://testuser:password@endpoint:123/name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM