Python-TypeError: not all arguments converted during string formatting

Where?

  运行Python程序,报错出现在这一行 return "Unknow Object of %s" % value

Why?

   %s 表示把 value变量装换为字符串,然而value值是Python元组,Python中元组不能直接通过%s 和 % 对其格式化,则报错

Way?

  使用 format 或 format_map 代替 % 进行格式化字符串

出错代码

def use_type(value):
    if type(value) == int:
        return "int"
    elif type(value) == float:
        return "float"
    else:
        return "Unknow Object of %s" % value

if __name__ == ‘__main__‘:
    print(use_type(10))
    # 传递了元组参数
    print(use_type((1, 3)))

改正代码

def use_type(value):
    if type(value) == int:
        return "int"
    elif type(value) == float:
        return "float"
    else:
        # format 方式
        return "Unknow Object of {value}".format(value=value)
        # format_map方式
        # return "Unknow Object of {value}".format_map({
        #     "value": value
        # })

if __name__ == ‘__main__‘:
    print(use_type(10))
    # 传递 元组参数
    print(use_type((1, 3)))

  

原文地址:https://www.cnblogs.com/2bjiujiu/p/9062115.html

时间: 2024-10-10 16:48:35

Python-TypeError: not all arguments converted during string formatting的相关文章

关于Python的TypeError not all arguments converted during string formatting

前言 在把yolov3的cfg文件转换为model_defs时,我忘记把str类型转换成int了,导致了一个错误,在此记录下来. 正文 如上图所示,'32'%2就是错误发生的地方. 我以为我拿到的是一个int类型的32,想判断它是偶数还是奇数. 实际上我拿到的是一个str类型的'32',这时python的解释器并没有把%理解成取余,而是理解成了这种东西. 我不知道"这种东西"的定义,但知道其用法和语法,其语法是这样的: name = 'cxy' print('%s is handsom

TypeError: not all arguments converted during string formatting

print ("So, you're 5r old, %r tall and %r heavy." % (age, height, weight)) print ("So, you're %r old, %r tall and %r heavy." % (age, height, weight)) 说明前后%和后面的参数数量不对应.红色就是错误的地方.

TypeError not all arguments converted during string formatt

1.错误描述 >>> a=1; >>> b=1; >>> for i in range(1,21): print('121d %121d' % (a,b)); if(i%3==0): a=a+b ; b=a+b; Traceback (most recent call last): File "<pyshell#23>", line 2, in <module> print('121d %121d' % (a

Python TypeError: not enough arguments for format string

今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_FORMAT(MIN(DATE),'%Y-%m-%d') AS MINDATE FROM (SELECT resource.PROJ,`day`.DATE FROM resource,`day` where resource.MAC=`day`.MAC ORDER BY PROJ) AS PROJSE

TypeError: not enough arguments for format string

Error msg: --- Logging error --- Traceback (most recent call last): File "/usr/lib/python3.6/logging/__init__.py", line 993, in emit msg = self.format(record) File "/usr/lib/python3.6/logging/__init__.py", line 839, in format return fm

python——TypeError: &#39;str&#39; does not support the buffer interface

import socket import sys port=51423 host="localhost" data=b"x"*10485760 #在字符串前加 b 是字符串变为bytes类. sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((host,port)) byteswritten=0 while byteswritten<len(data): startpos =

statsmodels.tsa.arima_model预测时报错TypeError: int() argument must be a string, a bytes-like object or a number, not &#39;Timestamp&#39;

在 python 中用 statsmodels创建 ARIMA 模型进行预测时间序列: import pandas as pd import statsmodels.api as sm df = pd.read_csv("data.csv", index_col=0, parse_dates=True) mod = sm.tsa.statespace.SARIMAX(df['price'], enforce_stationarity=False, enforce_invertibili

python TypeError: &amp;#39;builtin_function_or_method&amp;#39; object is not iterable keys

statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statinfo.st_size ] OneKey.append( OneFilePath ) AllFiles[ statinfo.st_size ] = OneKey else: if statinfo.st_size > MinSize: # print statinfo.st_size AllFiles

python TypeError: &#39;builtin_function_or_method&#39; object is not iterable keys

statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statinfo.st_size ] OneKey.append( OneFilePath ) AllFiles[ statinfo.st_size ] = OneKey else: if statinfo.st_size > MinSize: # print statinfo.st_size AllFiles