Python Forum
"invalid syntax" for no apparent reason - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "invalid syntax" for no apparent reason (/thread-13189.html)



"invalid syntax" for no apparent reason - wardancer84 - Oct-03-2018

i'm a littel stuck here, i get the mentionded error but i can not see why.

code:

#!/usr/bin/env python

'''
populates ansible inventory db from
aix registry db
'''

# -*- coding: utf-8 -*-

import os
import sys
import csv
import pymysql as db
try:
    import json
    from json import JSONDecodeError
except ImportError:
    import simplejson as json
    from simplejson import JSONDecodeError
import logging
logging.basicConfig(filename='/tmp/reg2inv.log',level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logger = logging.getLogger(__name__)


conn = db.connect(host='localhost', user='ansible', passwd='ansible', db='ansible_inv', charset='utf8')

c2 = conn.cursor()

try:
    csv_data = csv.reader(file('ansible_load_file.txt'))
    sql = """SELECT DISTINCT NODE FROM ansible_hosts_view;"""
    c2.execute(sql)
    csv_hosts = []
    for row in csv_data:
        csv_hosts.append(row[0])
    diff_to_del = (list(set(c2.fetchall()).difference(csv_hosts)))
    idx = 0
    for idx,row in enumerate(diff_to_del, start=1):
        logger.debug(diff_to_del)
        host = row
        sql = """DELETE FROM host WHERE  host='%s';""".format(host)
        c2.execute(sql % (host))
    logger.debug("delete:affected rows = {}".format(idx)
    for idx,row in enumerate(csv_data, start=1):
        host = row[0]
        hostname = row[3]
        var_data = {'inventory_lparname': host}
        var_json = json.dumps(var_data)
        ena = "1"
        sql = """INSERT INTO host(host, hostname, variables, enabled) VALUES ('%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE
        host='{}', hostname='{}',variables='{}',enabled='{}';""".format(host, hostname, var_json, ena)
        c2.execute(sql % (host, hostname, var_json, ena))
    logger.debug("insert:affected rows = {}".format(idx))
except db.Error,e:
    print e[0], e[1]
    c2.rollback()
    c2.close()

conn.commit()
conn.close()
error:

File "./reg2inv.py", line 44
for idx,row in enumerate(csv_data, start=1):
^
SyntaxError: invalid syntax

any ideas?


RE: "invalid syntax" for no apparent reason - volcano63 - Oct-03-2018

Add closing bracket on line 43.
In cases like that - look at preceding lines, un-balanced brackets are the most common case


RE: "invalid syntax" for no apparent reason - wardancer84 - Oct-03-2018

this fixed my issue, thanks!