Python join()方法

描述

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

语法

join()方法语法:

str.join(sequence)

参数

  • sequence -- 要连接的元素序列。

返回值

返回通过指定字符连接序列中元素后生成的新字符串。

实例

以下实例展示了join()的使用方法:

#!/usr/bin/python

str = "-";
seq = ("a", "b", "c"); # 字符串序列
print str.join( seq );

以上实例输出结果如下:

a-b-c

实例2:

for field in obj._meta.fields:
if update_fields is not None and field.name not in update_fields:
continue
property_name = ''.join(x.capitalize() for x in field.name.split('_'))
property_name = property_name[0].lower() + property_name[1:]
if property_name in src_obj:
value = src_obj[property_name]
if value is not None and value != '':
tp = type(field)
if tp in (IntegerField, BigIntegerField) and \
is_int(value):
setattr(obj, field.name, int(value))
elif tp in (DecimalField, ) and is_decimal(value):
setattr(obj, field.name, float(value))
elif tp in (CharField, ):
# 如果是个数组,则需要合并数组
if isinstance(value, list):
setattr(obj, field.name, u','.join(unicode(v) for v in value))
else:
setattr(obj, field.name, value[:field.max_length])
elif tp in (TextField, ):
setattr(obj, field.name, value)
elif tp in (DateField, ):
setattr(obj, field.name, datetime.datetime.strptime(value, MTIME_DATE_FORMAT))
elif tp in (DateTimeField, ):
setattr(obj, field.name, datetime.datetime.strptime(value, MTIME_DATETIME_FORMAT))
elif tp in (ForeignKey, ):
setattr(obj, field.name, value)
return obj
 
上一篇:Sublime Text 2 入门及技巧


下一篇:CreateForm(