【问题标题】:please help me identify the meaning of this error请帮我确定这个错误的含义
【发布时间】:2016-03-02 13:22:59
【问题描述】:

我正在尝试遍历保存在变量“decrypt”中的解密文件的内容,并将这些内容写入 MySQL 数据库表 (data_1)。

请帮助我确定此错误的含义以及可能的解决方法。

>>> import gnupg
>>> import csv
>>> import MySQLdb
>>> import StringIO
>>> import io
>>> gpg = gnupg.GPG(gnupghome="/home/chefgit/Desktop/.gnupg")
>>> file = open("/home/chefgit/Desktop/file_10.gpg", 'r+')
>>> decrypt=gpg.decrypt_file(file)
>>> connect = MySQLdb.connect('localhost', 'chefgit', 'taku99', 'python_db_1')
>>> cursor = connect.cursor()
>>> sql = "INSERT INTO data_1 ('id', 'first_name', 'last_name', 'email','country', 'ip_address')VALUES('%d','%s','%s','%s','%s','%s')"
>>> csv_input=csv.reader(StringIO.StringIO(decrypt))
>>> for row in csv_input:
...   cursor.execute(sql,row)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 187, in execute
    query = query % tuple([db.literal(item) for item in args])
TypeError: %d format: a number is required, not str
>>> csv_input=csv.reader(StringIO.StringIO(decrypt))
>>> for row in csv_input:
...   cursor.executemany(sql, row)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 237, in executemany
    r = r + self.execute(query, a)
  File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 187, in execute
    query = query % tuple([db.literal(item) for item in args])
TypeError: %d format: a number is required, not str
>>> csv_input = csv.reader(StringIO.StringIO(decrypt))
>>> for row in csv_input:
...   print(row)
... 
['obin', 'Mitchell', 'rmitchell0@paginegialle.it', 'Czech Republic', '247.22.171.221']
['2', 'Kathy', 'Wheeler', 'kwheeler1@soup.io', 'Indonesia', '90.207.243.94']
['3', 'Johnny', 'Grant', 'jgrant2@delicious.com', 'Cyprus', '40.53.219.97']
['4', 'Henry', 'Dunn', 'hdunn3@upenn.edu', 'China', '75.234.225.243']
['5', 'Craig', 'Holmes', 'cholmes4@dailymotion.com', 'Ukraine', '218.128.169.131']
['6', 'Phillip', 'Shaw', 'pshaw5@archive.org', 'France', '96.80.252.82']
['7', 'Lillian', 'Peterson', 'lpeterson6@arstechnica.com', 'Russia', '104.156.33.182']
['8', 'Linda', 'Hall', 'lhall7@tinypic.com', 'China', '253.195.137.88']
['9', 'Denise', 'Patterson', 'dpatterson8@lycos.com', 'Kazakhstan', '71.170.34.130']
[]

【问题讨论】:

  • 感谢您的回复。那么如何在我的代码中解决这个问题。有什么想法吗?请帮忙。
  • 现在抛出这个错误:TypeError: not enough arguments for format string

标签: python mysql csv stringio


【解决方案1】:

您列出的一列是 ID,它期望数字作为值,并且您尝试在数据库中插入“obin”作为 ID 的值,因为数据库说

TypeError: %d format: a number is required, not str

【讨论】:

  • 我取出了包含“obin”的那一行。我运行相同的代码,它仍然给出相同的错误。请帮忙。
  • 这可能是因为最后一行读作“[ ]”是一个空列表
猜你喜欢
  • 2022-10-17
  • 2011-08-15
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多