Tag: string

将一串字节转换为一个int(python)

如何将一个字节的string转换为Python中的int? 像这样说: 'y\xcc\xa6\xbb' 我想出了一个聪明/愚蠢的做法: sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1])) 我知道必须有内置的东西,或在标准的库,这更简单地… 这不同于转换可以使用int(xxx,16) 的hex数字的string ,但是我想要转换一串实际的字节值。 更新: 我有点像詹姆斯的回答好一点,因为它不需要导入另一个模块,但格雷格的方法更快: >>> from timeit import Timer >>> Timer('struct.unpack("<L", "y\xcc\xa6\xbb")[0]', 'import struct').timeit() 0.36242198944091797 >>> Timer("int('y\xcc\xa6\xbb'.encode('hex'), 16)").timeit() 1.1432669162750244 我的哈克法: >>> Timer("sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1]))").timeit() 2.8819329738616943 进一步更新: 有人问道在导入另一个模块时会遇到什么问题。 那么导入一个模块不一定便宜,看看: >>> Timer("""import struct\nstruct.unpack(">L", "y\xcc\xa6\xbb")[0]""").timeit() […]

string格式命名参数?

我知道这是一个非常简单的问题,但我不知道如何谷歌。 我能怎么做 print '<a href="%s">%s</a>' % (my_url) 那my_url被使用了两次? 我假设我必须“命名” %s ,然后在参数中使用字典,但我不确定正确的语法? 只是供参考,我知道我可以在params中使用my_url两次,但这不是重点:)

如何编写符合PEP8的非常长的string,防止E501

由于PEP8build议保持低于你的Python程序的80列规则,我怎么能遵守与长string,即 s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten." 我将如何去扩大这个以下的行,即 s = "this is my really, really, really, really, really, really" + "really long string that I'd like to shorten."

JavaScript有一个内置的stringbuilder类?

我看到几个代码项目解决scheme : 但想看看是否有一个正常的JavaScript实现?

在Python 2.4连接string?

如何在python中连接string? 例如: Section = 'C_type' 将它与Sec_连接形成string: Sec_C_type

Java中的stringisNullOrEmpty?

这确实已经被问过,但谷歌没有find它 。 有没有,在任何标准的Java库(包括Apache /谷歌/ …),静态isNullOrEmpty()方法的Strings ?

如何删除string中的所有前导零

如果我有一个string 00020300504 00000234892839 000239074 我怎么能摆脱前面的零,这样我才会有这个 20300504 234892839 239074 注意上面的数字是随机生成的。

PHP函数使slu((URLstring)

function gen_slug($str){ # special accents $a = array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','Ð','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','?','?','J','j','K','k','L','l','L','l','L','l','?','?','L','l','N','n','N','n','N','n','?','O','o','O','o','O','o','Œ','œ','R','r','R','r','R','r','S','s','S','s','S','s','Š','š','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Ÿ','Z','z','Z','z','Ž','ž','?','ƒ','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','?','?','?','?','?','?'); $b = array('A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','D','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','IJ','ij','J','j','K','k','L','l','L','l','L','l','L','l','l','l','N','n','N','n','N','n','n','O','o','O','o','O','o','OE','oe','R','r','R','r','R','r','S','s','S','s','S','s','S','s','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Y','Z','z','Z','z','Z','z','s','f','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','A','a','AE','ae','O','o'); return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/','/[ -]+/','/^-|-$/'),array('','-',''),str_replace($a,$b,$str))); } 很好的工作,但我发现一些失败的案例: gen_slug('andrés')返回andras而不是andres 为什么? 关于preg_replace参数的任何想法?

如何将pythondate时间转换为可读格式date的string?

t = e['updated_parsed'] dt = datetime.datetime(t[0],t[1],t[2],t[3],t[4],t[5],t[6] print dt >>>2010-01-28 08:39:49.000003 我怎么把它变成一个string? "January 28, 2010"

用户消息中的多个

很多时候,当生成消息展示给用户时,消息中会包含一些我想告诉客户的东西 。 我举个例子:客户从1开始select了一些项目,并点击删除。 现在我想给客户一个确认信息,我想提及他select的项目数量,以便通过select一堆项目来最小化他犯错误的机会,并且当他只想删除其中的一个时点击删除他们。 一种方法是使通用消息如下所示: int noofitemsselected = SomeFunction(); string message = "You have selected " + noofitemsselected + " item(s). Are you sure you want to delete it/them?"; 这里的“问题”是noofitemselected为1的情况,我们必须写项目而不是项目和它们 。 我的正常解决scheme将是这样的 int noofitemsselected = SomeFunction(); string message = "You have selected " + noofitemsselected + " " + (noofitemsselected==1?"item" : "items") + ". Are […]