草庐IT

str_buff

全部标签

python - 对象类型 <class 'str' > 无法传递给 C 代码 - 虚拟环境

我正在使用MacAnaconda。我尝试使用Crypto的AES。但是,我遇到了一个奇怪的问题。我只想执行一行简单的代码:obj=AES.new('Thisisakey123',AES.MODE_CBC,'ThisisanIV456')如果我在没有虚拟环境的情况下运行代码,就可以了。$pythonPython3.6.4|Anaconda,Inc.|(default,Jan162018,12:04:33)[GCC4.2.1CompatibleClang4.0.1(tags/RELEASE_401/final)]ondarwinType"help","copyright","credits

python - 使用 __str__ 方法加入 python 对象列表

这个问题在这里已经有了答案:Howtoconcatenate(join)itemsinalisttoasinglestring(11个答案)关闭4个月前。我已经看过thisquestiononrepresentingstringsinPython但我的问题略有不同。代码如下:>>>classWeirdThing(object):...def__init__(self):...self.me=time.time()...def__str__(self):...return"%s"%self.me...def__repr__(self):...return";%s;"%self.me...

python 3 : Making a str object callable

我有一个接受用户输入的Python程序。我将用户输入存储在一个名为“userInput”的字符串变量中。我希望能够调用用户输入的字符串...userInput=input("Enteracommand:")userInput()由此,我得到错误:TypeError:'str'objectisnotcallable目前,我的程序正在做这样的事情:userInput=input("Enteracommand:")ifuserInput=='example_command':example_command()defexample_command():print('HelloWorld!')显

python:组合排序键函数 itemgetter 和 str.lower

我想按字典键对字典列表进行排序,但我不想区分大小写字符。dict1={'name':'peter','phone':'12355'}dict2={'name':'Paul','phone':'545435'}dict3={'name':'klaus','phone':'55345'}dict4={'name':'Krishna','phone':'12345'}dict5={'name':'Ali','phone':'53453'}dict6={'name':'Hans','phone':'765756'}list_of_dicts=[dict1,dict2,dict3,dict4,d

python - TypeError : must be string without null bytes, 不是 str

我正在尝试运行这段代码,对我拥有的每一帧运行相同的命令(几乎没有变化):traj.reset()importos#os.chdir(outname)fori,frameinenumerate(traj):frame.superpose()comando="pythonhollow.py-cconstraint-ohollow_%s.pdburei%s.pdb"%(i,i)os.system(comando)pml_cmd="pymolurei%s.pdbhollow_%s.pdb-c-d'ascartoon,urei%s;colorgray90,urei%s;centerchainA;

python - 如何在 python str.format 中转义点

我希望能够访问字典中带有str.format()点的键。我该怎么做?例如,不带点的键格式:>>>"{hello}".format(**{'hello':'2'})'2'但是当键中有一个点时它不会:>>>"{hello.world}".format(**{'hello.world':'2'})Traceback(mostrecentcalllast):File"",line1,inKeyError:'hello' 最佳答案 你不能。FormatStringSyntax支持仅整数或有效的Python标识符作为键。来自文档:arg_nam

python - 如何检查 __str__ 是否由对象实现

如果对象尚未实现__str__方法,我想在该对象上动态实现它。我尝试使用hasattr(obj,'__str__')它总是返回true,因为它从对象类中获取它。有没有办法确定对象是否实际实现了__str__?我知道我可以使用inspect.getmembers(obj)但我正在寻找更pythonic的方式编辑classEmployee(object):def__init__(self,name,age,emp_code):self.name=nameself.age=ageself.emp_code=emp_code测试e=Employee("A",23,"E1")printhasat

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

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

python - Python 中 str.replace 函数的大 O 表示法是什么?

str.replace的大Oh表示法是什么?Python中的函数?它总是O(n)吗?str="thisisstringexample"printstr.replace("is","was")thwaswasstringexample 最佳答案 大O符号是在最坏情况下计算的,最坏情况下的Python源代码只是“找到substr的下一个位置,替换,然后走得更远”。一个替换执行O(n)操作(复制字符串)。一搜,根据http://effbot.org/zone/stringlib.htm,在最坏的情况下执行O(n*m)操作。由于它最多可以替

python - 为什么 f 字符串比 str() 解析值更快?

我在玩弄f字符串(参见PEP498),我决定比较一下f字符串解析的速度(例如f"{1}")使用通常的str解析(例如str(1))。但令我惊讶的是,当我使用timeit检查这两种方法的速度时函数,我发现f弦更快。>>>fromtimeitimporttimeit>>>timeit("f'{1}'")0.1678762999999961鉴于>>>timeit("str(1)")0.3216999999999999甚至是reprfunc,在大多数情况下它比strcast更快>>>timeit("repr(1)")0.2528296999999995请问这是为什么?我认为f弦在内部称为str