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.
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)
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 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