有人知道如何将tis PHP函数移植到python吗?
/**
* converts id (media id) to the corresponding folder in the data-storage
* eg: default mp3 file with id 120105 is stored in
* /(storage root)/12/105/default.mp3
* if absolute paths are needed give path for $base
*/
public static function id_to_location($id, $base = FALSE)
{
$idl = sprintf("%012s",$id);
return $base . (int)substr ($idl,0,4) . '/'. (int)substr($idl,4,4) . '/' . (int)substr ($idl,8,4);
}
在一行中(Python 2.x):
然后:
您想对Python3中的字符串使用format()方法:
http://docs.python.org/library/string.html#formatstrings
或者查看Python 2.X的字符串插值文档
http://docs.python.org/library/stdtypes.html
对于Python2.x,有以下选项:
[最佳选择]较新的str.format和完整的format specification,例如
较旧的interpolation formatting语法,例如
string.Template,例如
相关问题 更多 >
编程相关推荐