【问题标题】:syntax error unexpected character after line continuation character python行继续字符python之后的语法错误意外字符
【发布时间】:2012-11-27 10:15:02
【问题描述】:

我的代码是,

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"

但是我收到了这个错误,

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

但在 .所以请告诉我,为什么我会收到这个错误

错误图像是,

【问题讨论】:

标签: python character syntax-error


【解决方案1】:

您的代码都在一行中,但您使用的是续行转义字符 (\)。删除该字符:

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

当你在括号或括号内换行时,你根本不需要使用这样的换行技巧,你可以安全地将语句放在多行没有它:

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

【讨论】:

  • 谢谢,删除该字符后它起作用了。但我对 python 和 mongodb 的连接感到非常沮丧。昨天没用\的时候报错,今天我的一些代码在下一行,不需要用\。现在我在想为什么它显示我的代码分成两行。我在浏览器中看到使用 cPanelX 的代码
  • 你的行被终端或编辑器换行了,那里没有真正的换行符,只是一个 hell 大量多余的空格。
【解决方案2】:

您不需要在 []、{} 或 () 中使用换行符。如果您想不使用 \

,请随意将参数放在多行中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2017-08-22
    相关资源
    最近更新 更多