python-数据清洗与编码解码
0x01 join
str = ‘hk$$yicunyiye$$hello world‘
print(str.split(‘$$‘))
#自己实现
result = ‘‘
for i in str.split(‘$$‘):
result += i+‘ ‘
print(result)
#内置函数
print(‘,‘.join(str.split(‘$$‘)))
输出
[‘hk‘, ‘yicunyiye‘, ‘hello world‘]
hk yicunyiye hello world
hk,yicunyiye,hello world
0x02 endswith
str = [‘a.py‘,‘b.exe‘,‘c.e‘,‘d.erl‘]
for s in str:
if s.endswith(‘.py‘):
print(s)
输出a.py
0x03 startswith
str = [‘a.py‘,‘b.exe‘,‘c.e‘,‘d.erl‘]
for s in str:
if s.startswith(‘a‘):
print(s)
输出a.py
0x04 多重替换maketrans和translate
intab = ‘aeiou‘
outtab = ‘12345‘
trantab = str.maketrans(intab,outtab)
result = "this is string example...wow!!"
print(result.translate(trantab))
输出
th3s 3s str3ng 2x1mpl2…w4w!!
0x05 replace单次替换
#单次替换
print(result.replace(‘i‘,‘1‘).replace(‘e‘,‘2‘))
输出
th1s 1s str1ng 2xampl2…wow!!
0x06 strip(去掉空格)
string = ‘ yicunyiye ‘
print(string.strip())
//类似于
print(string.lstrip())
print(string.rstrip())
0x07 format 和 f‘
name=‘yicunyiye‘
age=20
string = ‘name is {}; age is {};‘.format(name,age)
print(string)
and
name=‘yicunyiye‘
age=20
string = f‘name is {name}; age is {age};‘
print(string)
输出
name is yicunyiye; age is 20;
python-数据清洗与编码解码
原文:https://www.cnblogs.com/yicunyiye/p/13632830.html