数据以格式呈现
tags_list = [
['foo'],
['foo', 'boo', 'goo'],
['boo', 'zoo']
]
如何填写 tags TEXT[]
列?
我正在尝试做类似的事情
cursor.executemany("""INSERT INTO tags_table VALUES (%s);""", tags_list)
但它会抛出异常not all arguments converted during string formatting
。
当然,可以通过任何方式修改输入数据格式。
更新
好的,我这样做:
for tags in tags_list:
tags_literal = '{"' + '","'.join(tags) + '"}'
cursor.execute("""INSERT INTO tags_data VALUES (%s);""", (tags_literal,))
但是表格中的行看起来像这样:
(['foo'],)
(['foo', 'boo', 'goo'],)
(['boo', 'zoo'],)
当我期待的时候:
{'foo'}
{'foo', 'boo', 'goo'}
{'boo', 'zoo'}
有什么想法吗? :)
最佳答案
按照我阅读的内容进行操作 here ,以下应该有效:
for tags in tags_list:
tags_with_quotes = ['"' + tag + '"' for tag in tags]
tags_literal = "{" + ",".join(tags_with_quotes) + "}"
cursor.execute("INSERT INTO tags_table VALUES (%s);", (tags_literal,))
关于python-3.x - 将列表写入列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47805085/