【问题标题】:pymsql + not enough arguments for format string + by a listmysql +格式字符串的参数不足+列表
【发布时间】:2017-12-28 23:23:58
【问题描述】:

我正面临错误消息

TypeError: not enough arguments for format string

这是我的代码

for data in zip(link_hash, link, headline, snippit, rubID, date, time):
    pass

if not sql_one_empty:
    sql_insert_hash = """ INSERT INTO ntv (link_hash, link, headline, snippit, rubID, date, time) VALUES (%s, %s, %s, %s, %s, %s, %s)"""

    cur.executemany(sql_insert_hash, data)
else:
    pass

完整的错误回溯:

Traceback (most recent call last):
  File "/home/unixben/Development/python/mySQL_save.py", line 45, in <module>
    cur.executemany(sql_insert_hash, data)
  File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 193, in executemany
    self._get_db().encoding)
  File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 209, in _do_execute_many
    v = values % escape(next(args), conn)
TypeError: not enough arguments for format string

有人知道吗?

【问题讨论】:

    标签: python mysql executemany


    【解决方案1】:

    数据应包含 7 个元素(每个 %s 一个)。应该不会吧。

    【讨论】:

      【解决方案2】:

      所以我有几个 txt 文件,我由

      with open("temp_link.txt") as temp_link, \
           open("temp_LinkHash.txt") as temp_hash, \
           open("temp_headline.txt") as temp_headline, \
           open("temp_snippet.txt") as temp_snippit, \
           open("temp_rubID.txt") as temp_rubID, \
           open("temp_date.txt") as temp_date, \
           open("temp_time.txt") as temp_time:
           link_url = temp_link.readlines()
           hash_url = temp_hash.readlines()
           headline = temp_headline.readlines()
           snippit = temp_snippit.readlines()
           rubID = temp_date.readlines()
           date = temp_time.readlines()
           time = temp_rubID.readlines()
      

      加载,然后通过 zip 函数合并,但不幸的是,我完全收到上述错误消息,是否有 7 个字段,因为“executemany”而完全符合预期?因为readlines()返回一个列表

      with open("temp_link.txt") as temp_link, \
           open("temp_LinkHash.txt") as temp_hash, \
           open("temp_headline.txt") as temp_headline, \
           open("temp_snippet.txt") as temp_snippit, \
           open("temp_rubID.txt") as temp_rubID, \
           open("temp_date.txt") as temp_date, \
           open("temp_time.txt") as temp_time:
           link_url = temp_link.readlines()
           hash_url = temp_hash.readlines()
           headline = temp_headline.readlines()
           snippit = temp_snippit.readlines()
           rubID = temp_date.readlines()
           date = temp_time.readlines()
           time = temp_rubID.readlines()
      
      
      for data in zip(hash_url, link_url, headline, snippit, rubID, date, time):
          pass
      
      print(data)
      
      
      if not sql_one_empty:
          sql_insert_hash = " INSERT INTO ntv (hash_url, link_url, headline, snippet, rub_id, datum, time) VALUES (%s, %s, %s, %s, %s, %s, %s)"
          cur.executemany(sql_insert_hash, data)
          db.commit()
      else:
          pass
      

      我真的有点绝望,用MySQL的界面

      【讨论】:

        猜你喜欢
        • 2015-10-16
        • 2019-01-17
        • 2017-12-18
        • 2019-07-23
        • 2018-06-07
        • 2015-11-16
        • 2021-04-01
        • 2013-10-27
        相关资源
        最近更新 更多