Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)

Introduction

When I searched the Python version of the function I wrote the day before to start from the part I was interested in, what would happen if I made a server program written in PHP in Python, it was too easy, but I will leave a note. It may be too easy for anyone writing Python, but if you're migrating PHP to Python, it's easier to start at this point.

First, split the WEB link by a specific character string

01.py


string = "http://www.sharp.co.jp/support/refrigerator/doc/sjd23d_mn.pdf?productId=SJ-D23D&_ga=2.1612.1531209133-1752366186.1522914385"
#Find the position of the string
s = string.find('.pdf')
print (s)
#57 will be returned
#A little experiment here.
s = string.find('doc')
#44 will be returned.
s = string.find('.doc')
# -1 will be returned. It identifies special characters. (Impressed by this degree)
After a little research, I found a function split () that could be used. let's do it.

To be honest, it's too easy (laughs)

02.py


string = "http://www.sharp.co.jp/support/refrigerator/doc/sjd23d_mn.pdf?productId=SJ-D23D&_ga=2.1612.1531209133-1752366186.1522914385"
print (string.split('.pdf'))
#The following will be returned. this is.It's the left and right divided by pdf.
# ['http://www.sharp.co.jp/support/refrigerator/doc/sjd23d_mn', '?productId=SJ-D23D&_ga=2.1612.1531209133-1752366186.1522914385']
print (string.split('.pdf')[0])
# http://www.sharp.co.jp/support/refrigerator/doc/sjd23d_mn This is the string you want.
string = string.split('.pdf')[0] + '.pdf'
print (string)
# http://www.sharp.co.jp/support/refrigerator/doc/sjd23d_mn.pdf This is all you need.
It is a branch under the following conditions, but the behavior of special characters is easy because there is no problem with find ().

It was really simple to write.

03.py


string = "http://www.sharp.co.jp/support/refrigerator/doc/sjd23d_mn.pdf?productId=SJ-D23D&_ga=2.1612.1531209133-1752366186.1522914385"
if string.find('.docx') > 0:
	    string = string.split('.docx')[0] + '.docx'
elif string.find('.doc') > 0:
	    string = string.split('.doc')[0] + '.doc'
elif string.find('.xlsx') > 0:
	    string = string.split('.xlsx')[0] + '.xlsx'
elif string.find('.xls') > 0:
	    string = string.split('.xls')[0] + '.xls'
elif string.find('.pptx') > 0:
	    string = string.split('.pptx')[0] + '.pptx'
elif string.find('.ppt') > 0:
	    string = string.split('.ppt')[0] + '.ppt'
elif string.find('.pdf') > 0:
	    string = string.split('.pdf')[0] + '.pdf'
print (string)
# http://www.sharp.co.jp/support/refrigerator/doc/sjd23d_mn.pdf It will be returned without any problem.

With PHP, you can do a little trouble with this alone. The popularity of Python will be this easy. As you probably don't know much about it these days, I feel that Pyhton is similar to the "xBase language" that was popular about 30 years ago. It seems that there was a conversion site for the migration from PHP to Python for a while, but it seems that there is no such site, so I will continue to write a memo of the migration for myself.

Recommended Posts

Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Cheating from PHP to Python
Software that saves the set character string to the clipboard when you click the [Python] button
How to quickly count the frequency of appearance of characters from a character string in Python?
How to get the Python version
The story that the version of python 3.7.7 was not adapted to Heroku
[Introduction to Python] How to split a character string with the split function
Try to extract a character string from an image with Python3
Outputs a line containing the specified character string from a text file
How to extract the desired character string from a line 4 commands
Change the active version in Pyenv from anaconda to plain Python
I want to extract an arbitrary URL from the character string of the html source with python
# 5 [python3] Extract characters from a character string
[Python] How to invert a character string
[Pandas] Expand the character string to DataFrame
[Introduction to Python] How to write a character string with the format function
[Introduction to Python] Thorough explanation of the character string type used in Python!
A solution to the problem that the Python version in Conda cannot be changed
[AWS] Detects the specified character string from the Lambda execution log and notifies slack
Divides the character string by the specified number of characters. In Ruby and Python.
[Question] Python string replacement.replace () will replace even the ones that are not specified.
[Python] I tried to get the type name as a string from the type function
Examples and solutions that the Python version specified in pyenv does not run
From the initial state of CentOS8 to running php python perl ruby with nginx
A story that failed when trying to remove the suffix from the string with rstrip
A python script that gets the number of jobs for a specified condition from indeed.com
A story that got stuck when trying to upgrade the Python version on GCE
[Ansible] Example of playbook that adds a character string to the first line of the file
[Python] Leave only the elements that start with a specific character string in the array
I raised the Python version from 2 to 3, but every time I restart the ubuntu terminal, the version remains 2.
What to do if Python does not switch from the System version in pyenv
Pass an array from PHP to PYTHON and do numpy processing to get the result
[Python] How to remove duplicate values from the list
The wall of changing the Django service from Python 2.7 to Python 3
How to delete the specified string with the sed command! !! !!
[Python] How to expand variables in a character string
# Function that returns the character code of a string
I wanted to use the Python library from MATLAB
Find the part that is 575 from Wikipedia in Python
PHP and Python samples that hit the ChatWork API
The first step to getting Blender available from Python
I felt that I ported the Python code to C ++ 98.
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
I tried to summarize the string operations of Python
A Python script that goes from Google search to saving the Search results page at once
A Python script that allows you to check the status of the server from your browser
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Python3] Format the character string using the variable name as the key.
[Python] I started Poetry & Impression that I moved from Pipenv to poetry
Remove double-byte spaces before and after the character string (python)
Get the package version to register with PyPI from Git
I tried face recognition from the video (OpenCV: python version)
Python learning basics ~ How to output (display) a character string? ~
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Program to determine leap year from the Christian era [Python]
Pass OpenCV data from the original C ++ library to Python
Extract the value closest to a value from a Python list element
Summary of character string format in Python3 Whether to live with the old model or the new model
I made something with python that NOW LOADING moves from left to right on the terminal