Python3 字符串处理方法

字符串处理

List 拼接成字符串:

list = ['a','b','c']
','.join(list)

字符串内容替换:

str = 'abc123 / / 123'
str.replace('/',' ')

字符串正则替换:

import re
...

str = 'abc123 / / 123'
res.sub('/s+',' ',str)

正则提取:

content = "【hello】world"
match = re.match('【(.*)】', content, re.UNICODE)
if match:
    print match.group(1)

字符串转换

字符串转换成 byte 数组:

s = "Hello, world!"   # str object 

print('str --> bytes')
print(bytes(s, encoding="utf8"))

b = b"Hello, world!"  # bytes object
print('\nbytes --> str')
print(str(b, encoding="utf-8"))

json 字符串处理

序列化为 json 字符串:

import json
di = dict(name='BOb', age=20, score=93)
json.dumps(di)

json 字符串反序列化:

import json
json_str = '{"name": "BOb", "age": 20, "score": 93}'
json.loads(json_str)

参考


微信公众号:我的网课 微信公众号:我的资产

results matching ""

    No results matching ""