删除管道前的所有字符,并在 python 中使用正则表达式删除管道
Remove all characters before a pipe and also remove pipe using regex in python
您好,我在工作,我正在 pandas 工作,并尝试删除此 csv 文件中此管道之前的所有字符。用竖线代替分号也会很有帮助。
Size| Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5xXlarge;
Size| Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5xlarge;
Sizes| Small - ( only one mic tab); Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5Xlarge;
Sizes| Small - ( only one mic tab); Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5Xlarge;
这是我一直在尝试的方法,但在逃离管道时遇到了问题。
df['Variations'] = df['Variations'].replace(regex=r'/\|$', value='')
我需要得到这个
Medium|Large|Xlarge|2Xlarge|3Xlarge|4Xlarge|5xXlarge
Medium|Large|Xlarge|2Xlarge|3Xlarge|4Xlarge|5xlarge
您可以使用
data['Variations'] = data['Variations'].str.replace(r'^[^|]*\|\s*|;\s*$', '').str.replace('\s*;\s*', '|')
.replace(r'^[^|]*\|\s*|;\s*$', '')
将删除从字符串开头到第一个 |
的所有子字符串,包括它和任何后续的空白字符和最后的 ;
(末尾有任何 0+ 空白) 和 .replace('\s*;\s*', '|')
会将所有 ;
替换为带有管道字符的分号周围的任何空格。
您好,我在工作,我正在 pandas 工作,并尝试删除此 csv 文件中此管道之前的所有字符。用竖线代替分号也会很有帮助。
Size| Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5xXlarge;
Size| Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5xlarge;
Sizes| Small - ( only one mic tab); Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5Xlarge;
Sizes| Small - ( only one mic tab); Medium; Large; Xlarge; 2Xlarge; 3Xlarge; 4Xlarge; 5Xlarge;
这是我一直在尝试的方法,但在逃离管道时遇到了问题。
df['Variations'] = df['Variations'].replace(regex=r'/\|$', value='')
我需要得到这个
Medium|Large|Xlarge|2Xlarge|3Xlarge|4Xlarge|5xXlarge
Medium|Large|Xlarge|2Xlarge|3Xlarge|4Xlarge|5xlarge
您可以使用
data['Variations'] = data['Variations'].str.replace(r'^[^|]*\|\s*|;\s*$', '').str.replace('\s*;\s*', '|')
.replace(r'^[^|]*\|\s*|;\s*$', '')
将删除从字符串开头到第一个 |
的所有子字符串,包括它和任何后续的空白字符和最后的 ;
(末尾有任何 0+ 空白) 和 .replace('\s*;\s*', '|')
会将所有 ;
替换为带有管道字符的分号周围的任何空格。