python进阶-16.pandas - 字符串处理

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from numpy import nan as NA
import re

3.11 字符串操作

str1 = 'a ,  b  , c'

思路: strip(obj) 去除左右字符, 默认空 ,也可指定 字符obj
将 str1 变成去除空格的列表 [‘a’,‘b’,‘c’]

result1 = [each.strip() for each in str1.split(',')]
result1
['a', 'b', 'c']
list2 = ['#123#','#456','789#']
  • 将 list2的 数据的 # 去掉
result2 = [each.strip('#') for each in list2]
result2
['123', '456', '789']
list3 = ['#123#','#456','789#','@aaa','@bbb@']
  • 将 list3的 数据的 # @去掉
result3 = [each.strip('#').strip('@') for each in list3]
result3
['123', '456', '789', 'aaa', 'bbb']

你可能感兴趣的:(列表,python,字符串)