string
1 lstrip rstrip strip
lstrip = left strip =去除(字符串)左边的=stip leading=去除(字符串)开始的
rstrip = right strip =去除(字符串)右边的=strip trailling=去除(字符串)末尾的
strip = stip left and right = 去除(字符串的)左边的和右边的=strip leading and trailing = 去除(字符串)开始的和末尾的
默认为白空格Whitespace。
所谓的白空格,一般指的是:
import sys # 1
reload(sys) # 2
sys.setdefaultencoding('utf-8') # 3
1 lstrip rstrip strip
lstrip = left strip =去除(字符串)左边的=stip leading=去除(字符串)开始的
rstrip = right strip =去除(字符串)右边的=strip trailling=去除(字符串)末尾的
strip = stip left and right = 去除(字符串的)左边的和右边的=strip leading and trailing = 去除(字符串)开始的和末尾的
默认为白空格Whitespace。
所谓的白空格,一般指的是:
空格本身,回车\r,换行\n,制表符\t, 换页符\f,(对应于正则表达式中的: \s == [ \r\n\t\f] ) 。
保存字符串到文件
def save_to_file(file_name, contents):
fh = open(file_name, 'w')
fh.write(contents)
fh.close()
save_to_file('mobiles.txt', 'your contents str')
解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
import sys # 1
reload(sys) # 2
sys.setdefaultencoding('utf-8') # 3