TypeError:并非在字符串格式化过程中转换的所有参数在元组python

时间:2018-05-31 00:28:32

标签: python

我试图在DB中保存一堆元组

 cursor = cnx.cursor()
         query = """INSERT INTO `TableA`
                     (`clientid`,
                    `createddatetime`,
                    `siteid`,...)
                 VALUES(?,?,?,...)"""

    cursor.executemany(query, listTosave)

我的 listTosave 包含像;

这样的元组列表
  

[(' AS0001',' 1170',1,' 1','未知',442,1,datetime.datetime (2018年,   5,28,23,0),datetime.datetime(2018,3,15,11,15),   datetime.datetime(2018,3,15,10,56),datetime.datetime(2018,5,28,   23,18,26),datetime.datetime(2018,5,28,23,59,22),   十进制(' 15177.3184'),十进制(' 15185.7562'),十进制(' 8.4378'),   十进制(' 1313.0547'),十进制(' 1313.6179'),十进制(' 0.5632'),   十进制(' 0.0000'),十进制(' 0.0000'),十进制(' 0.0000'),   十进制(' 0.0000'),十进制(' 0.0000'),十进制(' 0.0000'),   十进制(' 24.6518'),十进制(' 24.6518'),15101.7062,0.0,0.0,0.0,   24.6563),(...........)]

当我试图保存时,我得到了;

 File "/tmp/pymodules/pymysql/cursors.py", line 194, in executemany
  File "/tmp/pymodules/pymysql/cursors.py", line 194, in <genexpr>
  File "/tmp/pymodules/pymysql/cursors.py", line 163, in execute
  File "/tmp/pymodules/pymysql/cursors.py", line 142, in mogrify
TypeError: not all arguments converted during string formatting

为什么我会收到此错误?

编辑:我将日期时间对象/小数对象也转换为字符串。我的新名单就像;

  

[(&#39; AS0001&#39;,&#39; 1170&#39;,&#39; 1&#39;,&#39; 1&#39;,&#39;未知&#39;, &#39; 442&#39;,&#39; 1&#39;,&#39; 2018-05-28   23:00:00&#39;,&#39; 2018-03-15 11:15:00&#39;,&#39; 2018-03-15 10:56:00&#39;,&#39; 2018 -05-28   23:18:26&#39;,&#39; 2018-05-28 23:59:22&#39;,&#39; 15177.3184&#39;,&#39; 15185.7562&#39;,   &#39; 8.4378&#39;,&#39; 1313.0547&#39;,&#39; 1313.6179&#39;,&#39; 0.5632&#39;,&#39; 0.0000&#39;, &#39; 0.0000&#39 ;,   &#39; 0.0000&#39;,&#39; 0.0000&#39;,&#39; 0.0000&#39;,&#39; 0.0000&#39;,&#39; 24.6518&#39;, &#39; 24.6518&#39 ;,   &#39; 15101.7062&#39;,&#39; 0.0&#39;,&#39; 0.0&#39;,&#39; 0.0&#39;,&#39; 24.6563&#39;) ,(.....)]

但我仍然得到同样的错误

2 个答案:

答案 0 :(得分:1)

根据您返回的错误的猜测,它可能与存储在列表中的日期时间对象有关,而不是正确转换为字符串表示。在str()中包含这些内容可能是导致问题的原因。

请注意以下示例:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2018, 5, 31, 8, 42, 48, 172575)
>>> str(datetime.datetime.now())
'2018-05-31 08:42:53.192586'

另一个选项可能是通过将json.dumps列表元素用于json字符串格式来消除错误。但这取决于您希望如何将数据存储在数据库中。请参阅以下内容:

>>> import json
>>> element = (
... 'AS0001', '1170', '1', '1', 'Unknown', '442', '1', '2018-05-28 23:00:00', '2018-03-15 11:15:00', '2018-03-15 10:56:00',
... '2018-05-28 23:18:26', '2018-05-28 23:59:22', '15177.3184', '15185.7562', '8.4378', '1313.0547', '1313.6179', '0.5632',
... '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '24.6518', '24.6518', '15101.7062', '0.0', '0.0', '0.0',
... '24.6563')
>>> json.dumps(element)
'["AS0001", "1170", "1", "1", "Unknown", "442", "1", "2018-05-28 23:00:00", "2018-03-15 11:15:00", "2018-03-15 10:56:00", "2018-05-28 23:18:26", "2018-05-28 23:59:22", "15177.3184", "15185.7562", "8.4378", "1313.0547", "1313.6179", "0.5632", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000", "24.6518", "24.6518", "15101.7062", "0.0", "0.0", "0.0", "24.6563"]'

检索此数据时,您可以使用json.loads将其加载回python对象格式

答案 1 :(得分:0)

这是我工作的一个最小例子。

query = "INSERT INTO `pet`(`name`,`owner`) values(%s,%s)"
listTosave = [('some','shsjhs'),('sosos','shshsh')]
cursor.executemany(query, listTosave)

确保您有一个元组列表,并在查询字符串中使用%s