Python 到 SQL:列表的列表
我目前在将 python 列表导出到 mySQL DB 时遇到问题。 我正在使用 MySQLdb 进行相同的操作并使用executemany 命令。
例如,我有一个列表列表:
k1=[[['Bob', 'Alfred', 'Jen'],
['123 Elm Street', '55 Ninth Ave', '1 Paved Rd'],
[00123, 34565, 30094],
['Newark', 'Salinas', 'Los Angeles'],
['NJ', 'CA', 'CA']],[['Bob1', 'Alfred1', 'Jen1'],
['123 Elm Street1', '55 Ninth Ave1', '1 Paved Rd1'],
[001231, 345651, 300941],
['Newark1', 'Salinas1', 'Los Angeles1'],
['NJ1', 'CA1', 'CA1']]]
我尝试(在初始化光标和要使用的数据库之后):
cursor.executemany('''INSERT INTO addresses
(name, street, zipcode, city, state)
VALUES
(%s, %s, %s, %s, %s)''',zip(k1))
我得到一个错误:
_mysql_exceptions.ProgrammingError: not enough arguments for format string
,基本上,我希望将列表 k1 的列表中的名称、街道、邮政编码、城市和州存储到 mySQL 数据库表“地址”以及相应的字段条目中。
如果有人能指出我正确的方向,那就太好了。
I am currently facing a problem in exporting a python list of list into mySQL DB.
Iam using MySQLdb for the same and utilising the executemany command.
For example, I have a list of list:
k1=[[['Bob', 'Alfred', 'Jen'],
['123 Elm Street', '55 Ninth Ave', '1 Paved Rd'],
[00123, 34565, 30094],
['Newark', 'Salinas', 'Los Angeles'],
['NJ', 'CA', 'CA']],[['Bob1', 'Alfred1', 'Jen1'],
['123 Elm Street1', '55 Ninth Ave1', '1 Paved Rd1'],
[001231, 345651, 300941],
['Newark1', 'Salinas1', 'Los Angeles1'],
['NJ1', 'CA1', 'CA1']]]
and I try (after initialising the cursor and the database to use):
cursor.executemany('''INSERT INTO addresses
(name, street, zipcode, city, state)
VALUES
(%s, %s, %s, %s, %s)''',zip(k1))
and I get an error:
_mysql_exceptions.ProgrammingError: not enough arguments for format string
Well, basically, I would like the names, street, zipcode, city and state from the list of list k1 to mySQL db table "addresses" with the corresponding field entries.
Would be greatful if anyone could point me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的数据结构真的很尴尬。我假设您想将 k1 转换为:
现在您可以执行executemany:
Your datastructure is really awkward. I'm assuming you wanted to transform k1 into this:
Now you can do executemany:
我不认为这实际上是一个 SQL 问题;在尝试调用executemany() 之前,让我们集中精力以正确的形式获取数据。
您的 k1 示例是一个由 2 个列表组成的列表,每个内部列表也是一个列表列表,这些列表是名称/地址组件。所以你实际上有一个列表的列表。我不知道列表外层有什么意义,但内层似乎适合压缩。
忽略外层,我认为您想要的是
k1 中的第一个元素产生看起来正常的结果。同样,我不知道在 k1[0] 和 k1[1] 中重复相同数据的意图是什么,因此我不会尝试猜测您将如何处理 k1 中的其他元素。
同样,在担心如何将其与 SQL 数据库集成之前,只需尝试在 python 解释器中使用它:
I don't think this is really a SQL question; let's just concentrate on getting the data in the right form before you even try to call executemany().
Your k1 example is a list of 2 lists, each of which inner lists is also a list of lists, which are names/address components. So you actually have a list of lists of lists. I don't know what's the point of the outer layer of list, but the inner layer seems amenable to zip.
Ignoring that outer layer, I think what you want is
Which yields sane-looking results for the first element in k1. Again, I don't know what the intent is behind repeating the same data in k1[0] and k1[1] so I won't try to guess what you will do with the further elements in k1.
Again, before worrying about how to integrate this with your SQL database, just try playing with it in the python interpreter:
我认为你的意思是将 k1 中的每个列表(每个列表本身都包含一些列表)传递给 zip,如下所示:
另请注意,你的邮政编码应该是字符串,而不是数字,以便保留前导零,并避免使用它们被误解析为您不想要的东西(看到那里的“83”邮政编码?我相信是 00123)。
I think you mean to pass each list inside k1 (which each themselves contain a few lists) to zip, like this:
Note too that your zip codes should be strings, not numbers, in order to preserve the leading zeroes, and avoid having them misparsed as things you don't intend (see the '83' zip code in there? It was 00123 I believe).
如果你的数据结构看起来像这样:(不是格式化而是组织。你需要表示行而不是列来插入 SQL。)
你的 SQL INSERT 可能有机会工作。
If you get your data structure to look like this: (not formatting but organization. You need to represent rows rather than columns to do inserts into SQL.)
your SQL INSERT may have a chance of working.