A memorandum of python string deletion process

Overview

I got stuck a little when deleting a string in Python, so I wrote it as a reminder You may find yourself casually using rstrip, lstrip.

phenomenon

You may want to use lstrip or rstrip when removing prefixes or suffixes in filenames, as shown below.


fileName='test_Output file_20191217_001.xlsx'
#Remove prefix
print(fileName.lstrip('test_'))
#Output file_20191217_001.xlsx

#Remove suffix
print(fileName.rstrip('.xlsx'))
# test_Output file_20191217_001


There seems to be no problem as an output result, but a problem occurs when the character string is as follows.


fileName='test_est_Output file_20191217_001xsls.xlsx'
#Remove prefix
print(fileName.lstrip('test_'))
#Output file_20191217_001xsls.xlsx

#Remove suffix
print(fileName.rstrip('.xlsx'))
# test_est_Output file_20191217_001

I want only test_ or .xlsx to disappear, but other characters have also disappeared. The cause is that lstrip or rstrip deletes the one that matches the specified string.

For patterns erased with a prefix, one of the characters in test_ is deleted. If it is a suffix, one of the .xlsx is deleted.

If you want to remove the prefix or suffix in these cases It seems good to use re to remove it with a regular expression. In the case of the example

import re

fileName='test_Output file_20191217_001.xlsx'
#Remove prefix
print(re.sub("^test_", "", fileName))
#Output file_20191217_001.xlsx

#Remove suffix
print(re.sub(".xlsx$", "", fileName))
# test_Output file_20191217_001

You can firmly remove the prefix or suffix.

Examples that didn't work with lstrip or rstrip also work, as shown below.

import re

fileName='test_est_Output file_20191217_001xsls.xlsx'
#Remove prefix
print(re.sub("^test_", "", fileName))
# est_Output file_20191217_001xsls.xlsx


#Remove suffix
print(re.sub(".xlsx$", "", fileName))
# test_est_Output file_20191217_001xsls

Recommended Posts

A memorandum of python string deletion process
[Python] A memorandum of beautiful soup4
Daemonize a Python process
A memorandum of calling Python from Common Lisp
A memorandum of extraction by python bs4 request
A memorandum of kernel compilation
Cut a part of the string using a Python slice
[Python] Use a string sequence
A small memorandum of openpyxl
Basics of Python learning ~ What is a string literal? ~
A memorandum about Python mock
A memorandum of using eigen3
Find out the apparent width of a string in python
A memorandum of stumbling on my personal HEROKU & Python (Flask)
Python> Read from a multi-line string instead of a file> io.StringIO ()
A record of patching a python package
A good description of Python decorators
A brief summary of Python collections
Memorandum of beginners Python "isdigit" movement
Create a random string in Python
A memorandum of closure survey contents
A memorandum of understanding for the Python package management tool ez_setup
Create an instance of a predefined class from a string in Python
Python memorandum
Format when passing a long string as an argument of python
Python Memorandum 2
Python memorandum
python memorandum
A memorandum of scraping & machine learning [development technique] by Python (Chapter 4)
Python string
Python memorandum
Python memorandum
A memorandum regarding the acquisition of the Python3 engineer certification basic exam
Conversion of string <-> date (date, datetime) in Python
(Java, JavaScript, Python) Comparison of string processing
Display a list of alphabets in Python 3
A memorandum of using Python's input function
A memorandum of speed of arbitrary degree diagonalization
Make a relation diagram of Python module
Memorandum of python beginners About inclusion notation
# 5 [python3] Extract characters from a character string
Connect a lot of Python or and and
python string comparison / use'list'and'in' instead of'==' and'or'
A memorandum of understanding about django's QueryDict
[Python] How to invert a character string
[python] Get a list of instance variables
String conversion of a list containing numbers
[python] [meta] Is the type of python a type?
Basic grammar of Python3 system (character string)
Generate a class from a string in Python
The story of blackjack A processing (python)
[Python] Get a list of folders only
A memorandum of trouble when formatting data
I want to color a part of an Excel string in Python
A memo of a tutorial on running python on heroku
Python: String concatenation
Draw a graph of a quadratic function in Python
Introduction of Python
Python string format
Python basics memorandum
python string slice