草庐IT

format_ctx

全部标签

python - 值错误 : continuous format is not supported

我写了一个简单的函数,我正在使用average_precision_score从scikit-learn计算平均精度。我的代码:defcompute_average_precision(predictions,gold):gold_predictions=np.zeros(predictions.size,dtype=np.int)foridxinrange(gold):gold_predictions[idx]=1returnaverage_precision_score(predictions,gold_predictions)执行该函数时,会产生以下错误。Traceback(mo

python - 在 Django 中格式化日期时如何使用本地化的 "short format"?

我需要显示本地化格式的日期。如果我使用django.utils.formats.localize,返回的日期为“2012年6月11日”。我如何格式化日期以返回“06/11/2012”,并进行适当的本地化(例如,“英国的11/06/2012”)?我需要类似于Java的DateFormat.SHORT的东西.有类似的东西吗? 最佳答案 是的,有SHORT_DATE_FORMAT.在模板中,可以将它与datefilter一起使用:{{your_date_value|date:"SHORT_DATE_FORMAT"}}在模板之外,可以使用d

Python 字符串格式 - 旧 `%` 与新 `str.format`

新格式让我们可以做到这一点:'{:.-可选的填充字符。我们可以使用旧格式来做到这一点吗?(我知道我们可以用空格填充'%-12s'%'##')此外,旧格式让我们可以这样做:'%-*s'%(12,'##')-可变长度。我们可以使用新的格式来做到这一点吗? 最佳答案 要使用new-format实现可变长度,您可以使用替换嵌套->>>'{:{}>>'{:{}>>'{:{}偶数空格作为填充字符->>>'{:{}请注意,您并不总是需要使用替换嵌套,您也可以直接在格式中指定它们->>>'{:您还可以指定每个参数的位置来决定哪个参数去哪里。示例->

python - 使用 .format() 打印可变字符串和四舍五入的数字

我想打印这样的东西:你好¦7.16这是我正在使用的代码MyString='Hello'MyFloat=7.157777777print"{}¦{0:.2f}".format(MyString,MyFloat)但是我得到了错误:ValueError:cannotswitchfromautomaticfieldnumberingtomanualfieldspecification如果我尝试:MyString='Hello'MyFloat=7.157777777print"{s}¦{0:.2f}".format(MyString,MyFloat)或str而不是s我得到错误:KeyError:

Python "ValueError: incomplete format"打印 ("stuff %"% "thingy")

我使用此代码的目标是,当您输入某个数字时,系统会根据您键入的内容打印该数字和一些其他输出。出于某种原因,我这里给出的错误是“ValueError:格式不完整”。它与百分比有关。该错误是什么意思,我该如何解决?谢谢!variable="Blah"variable2="Blahblah"text=raw_input("Typesomestuff:")if"1"intext:print("One%"%variable)elif"2"intext:print("Two%"%variable2) 最佳答案 Python期望在字符串文字中的%之

python - Django 管理员 : how to format readonly fields?

我有一个模型,Director,带有两个DateField和两个子类(代码如下)。我正在尝试为每个Director创建一个管理页面,它显示相应的子类实例,而不是Director实例;这部分很简单(我为每个子类创建一个内联,给主ModelAdmin一个排除所有字段的表单,并让主ModelAdmin只从具有相应实例的内联请求表单集-代码;有一个Unresolved问题使用这种方法,我在下面指出,但不是这个问题的重点)。我遇到的问题是我想修改显示给用户的值,其中一个值显示在只读字段中,另一个则不是。处理是我想把一个魔法值(date(1,1,1))改成字符串"Onincorporation"。

python - 类型错误 : Mismatch between array dtype ('object' ) and format specifier ('%.18e' )

我有以下数组:X=np.array([image_array_to_vector1,image_array_to_vector2,image_array_to_vector3,image_array_to_vector4])X的打印结果如下:[array([167,167,169,...,1,1,1],dtype=uint8)array([42,43,43,...,41,36,34],dtype=uint8)array([0,0,0,...,0,0,0],dtype=uint8)array([0,0,0,...,0,0,0],dtype=uint8)]当我尝试将数据保存为txt时:X_

Python 正则表达式 :combining re pattern format with a variable

我想结合一个python变量和模式。我该怎么做?下面是我想做的。re.search(r'**some_variable+pattern**',str_for_pattern_match,flags)感谢您的帮助。 最佳答案 通常的字符串格式化方式效果很好re.search(r'**%s+pattern**'%some_variable,str_for_pattern_match,flags) 关于Python正则表达式:combiningrepatternformatwithavaria

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