简体   繁体   中英

syntax error unexpected character after line continuation character python

my code is,

import MySQLdb
import collections
from pymongo import Connection

from config.MySQLdb import *
from config.MongoDB import *
from config.SyncTables import *

db = MySQLdb.connect(DB_HOST,DB_USR,DB_PWD,DB_NAME)
cursor = db.cursor()
query = "SELECT * FROM table_name WHERE userid = 1"
cursor.execute(query)
row = cursor.fetchall()

mconn = Connection(MONGODB_HOST,MONGODB_PORT)
mdb = mconn[MONGODB_DBNAME]
#col = mdb[trngl_advertiser_agegroup]

db.trngl_advertiser_agegroup .insert({'id': row[0],'userid':row[1], 'ageid':row[2],       'age_value':row[3], \                                             'created':row[4],  'modified':row[5]})
print "after update"

but I'm getting this error,

triongle@triongle.com [~/download/DataInsertionScript]# python IngestDataToMongo.py

  File "IngestDataToMongo.py", line 21
    db.table_name.insert({'id': row[0],'userid':row[1], 'ageid':row[2],  'age_value':row[3],\                                                       'created':row[4], 'modified':row[5]})
                                                                                                                                                                                               ^

SyntaxError: unexpected character after line continuation character

But I don't see any unexpected character after . So please tell me, why I'm getting this error

The error image is, 在此处输入图片说明

Your code is all on one line, but you are using a line continuation escape character ( \\ ). Remove that character:

db.trngl_advertiser_agegroup .insert({'id': row[0],'userid':row[1], 'ageid':row[2], 'age_value':row[3], 'created':row[4],  'modified':row[5]})

You do not need to use such a line continuation trick at all when you break the line inside parenthesis or brackets, you can safely put the statement on multiple lines without it:

db.trngl_advertiser_agegroup .insert({
    'id': row[0],
    'userid':row[1], 
    'ageid':row[2], 
    'age_value':row[3],
    'created':row[4],
    'modified':row[5]
})

You don't need line continuation characters inside [], {}, or (). Feel free to put the arguments in multiple lines if you want without \\

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