相关疑难解决方法(0)

在元组定义中使用尾随逗号的语法规则是什么?

在单个元素元组的情况下,需要尾随逗号.

a = ('foo',)
Run Code Online (Sandbox Code Playgroud)

那个有多个元素的元组怎么样?似乎尾随逗号是否存在,它们都是有效的.它是否正确?在我看来,使用尾随逗号更容易编辑.这是一种糟糕的编码风格吗?

a = ('foo1', 'foo2')
b = ('foo1', 'foo2',)
Run Code Online (Sandbox Code Playgroud)

python syntax tuples

105
推荐指数
7
解决办法
4万
查看次数

TypeError:'int'对象不可迭代 - Python

我收到以下错误:

  File "/home/ec2-user/test/test_stats.py", line 43, in get_test_ids_for_id
    cursor.execute("""select test_id from test_logs where id = %s """, (id))
  File "/home/ec2-user/.etl/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
    query = query % tuple([db.literal(item) for item in args])
TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)

这是我的代码部分,我遇到了麻烦:

def get_test_ids_for_id(prod_mysql_conn, id):
    cursor = prod_mysql_conn.cursor()
    cursor.execute("""select test_id from test_logs where id = %s """, (id))
    rows = cursor.fetchall()
    test_ids = []
    for row in rows:
      test_ids.append(row[0])
    return test_ids
Run Code Online (Sandbox Code Playgroud)

python mysql mysql-python python-2.7

5
推荐指数
1
解决办法
7930
查看次数

标签 统计

python ×2

mysql ×1

mysql-python ×1

python-2.7 ×1

syntax ×1

tuples ×1