草庐IT

well-formatted

全部标签

python - Python 的 str.format() 方法的默认 kwarg 值

我正在尝试尽可能简单地保持现有字符串的多元化,并且想知道是否有可能让str.format()在寻找kwargs时解释默认值.这是一个例子:string="{number_of_sheep}sheep{has}runaway"dict_compiled_somewhere_else={'number_of_sheep':4,'has':'have'}string.format(**dict_compiled_somewhere_else)#gives"4sheephaverunaway"other_dict={'number_of_sheep':1}string.format(**oth

python - 值错误 : time data '%Y-%m-%d %H:%M:%S' does not match format '2012-11-14 14:32:30'

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭10年前。我正在尝试通过datetime.datetime.strptime将字符串'2012-11-1414:32:30'转换为datetime.datetime对象方法使用格式字符串'%Y-%m-%d%H:%M:%S'。这样做,我得到一个错误:ValueError:timedata'%Y-%m-%d%H:%M:%S'doesnotmatchformat'201

python - 如何将 tuple1 if ... else tuple2 传递给 str.format?

简单来说,为什么会出现以下错误?>>>yes=True>>>'no[{0}]yes[{1}]'.format(("","x")ifyeselse("x",""))Traceback(mostrecentcalllast):File"",line1,inIndexError:tupleindexoutofrange我使用的是python2.6。 最佳答案 ☞索引选项:在格式字符串中访问参数项时,应该使用索引来调用值:yes=Trueprint'no[{0[0]}]yes[{0[1]}]'.format(("","x")ifyesels

python - psycopg2 类型错误 : not all arguments converted during string formatting

我正在尝试执行一个简单的查询,但无论我如何传递参数都会出现此错误。这是查询(我正在使用Trac数据库对象连接到数据库):cursor.execute("""SELECTnameFROM"%s".customerWHEREfirm_id='%s'"""%(schema,each['id']))schema和each['id']都是简单的字符串print("""SELECTnameFROM"%s".customerWHEREfirm_id='%s'"""%(schema,each['id']))结果:SELECTnameFROM"Planing".customerWHEREfirm_id=

python - py.test : format failed assert AND print custom message

py.testassertdocs说...ifyouspecifyamessagewiththeassertionlikethis:asserta%2==0,"valuewasodd,shouldbeeven"thennoassertionintrospectiontakesplacesatallandthemessagewillbesimplyshowninthetraceback.Python的内置unittest模块也执行此操作,除非您的TestCase设置longMessage=True.拥有漂亮的断言格式对测试开发人员友好,而自定义消息对业务需求/人性化更友好。当您不在测试上

python - 错误 : Unsupported format, 或损坏的文件:预期的 BOF 记录

我正在尝试打开一个xlsx文件并打印其中的内容。我一直遇到这个错误:importxlrdbook=xlrd.open_workbook("file.xlsx")print"Thenumberofworksheetsis",book.nsheetsprint"Worksheetname(s):",book.sheet_names()printsh=book.sheet_by_index(0)printsh.name,sh.nrows,sh.ncolsprintprint"CellD30is",sh.cell_value(rowx=29,colx=3)printforrxinrange(5

python - python 中的 XML 解析 : expaterror not well-formed

我正在使用Python的xml.etree.ElementTree对文件进行一些XML解析。但是,我在文档中途遇到此错误:xml.parsers.expat.ExpatError:notwell-formed(invalidtoken):lineX,columnY所以我转到vim中的X行,Y列,我看到一个带红色背景突出显示的符号(&)。这是什么意思?而且它前面的两个字符是>>>,所以也许>>&有什么特别之处?有人知道怎么解决吗? 最佳答案 &是XML中的一个特殊字符,用于字符实体。如果您的XML本身有&,而不是作为&或ѐ等实体的一部

python - 使用 .format() 方法在 Python 3.3 中格式化要对齐的文本

我是Python的新手,正在尝试处理一些示例脚本。我正在做一个简单的收银机类型的事情,但我想证明或右对齐输出,使其看起来像这样:subTotal=24.95tax=subTotal*0.0725total=subTotal+taxpaid=30change=paid-totalprint("Thesubtotalwas:$",subTotal)print("Thetaxwas:$",tax)print("Thetotalwas:$",total)print("Thecustomerpaid:$",paid)print("Changedue:$",change)我知道我可以用更少的打印语

python - 属性错误 : 'NoneType' object has no attribute 'format'

print("HelloWorld")print("{}World").format(Hello)我正在开发我的第一个“HelloWorld”程序,我可以通过使用print函数和一个简单的字符串文本来让它工作,但是当我尝试使用.format时,它给出了我的错误:AttributeError:'NoneType'objecthasnoattribute'format'这是说我需要为.format初始化一个变量还是我遗漏了什么? 最佳答案 你的括号错了print("HelloWorld")print("{}World".format('

python - 惯用的 Python 日志记录 : format string + args list vs. 内联字符串格式 - 哪个是首选?

使用格式字符串+args列表调用日志记录函数与格式化内联是否有利?我看过(并写过)使用内联字符串格式的日志记录代码:logging.warn("%s%s%s"%(arg1,arg2,arg3))但我认为它更好(性能方面,更惯用)使用:logging.warn("%s%s%s",arg1,arg2,arg3)因为第二种形式在调用日志函数之前避免了字符串格式化操作。如果当前日志记录级别会过滤掉日志消息,则无需格式化,从而减少计算时间和内存分配。我是在正确的轨道上,还是错过了什么? 最佳答案 恕我直言,对于很可能会显示的消息,例如给err