EXTRACT NUMBERS FROM STRING:
string = "2 sweets are better than 10 laddus"
res=[int(i) for i in string.split() if i.isdigit()]
print(res)
o/p: [2,10]
EXTRACTING NUMBERS FROM LIST OF STRINGS
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
print("The original list : " + str(test_list))
res = [int(sub.split('.')[1]) for sub in test_list]
print("The list after Extracting numbers : " + str(res))
o/p: [24,18,8,21]
EXTRACTING DIGITS FROM STRING
teststring="242 is greater than 255"
res=[int(i) for i in teststring if i.isdigit()]
print(res)
o/p: [2,4,2,2,5,5]
EXTRACTING WORDS FROM STRING
import re
teststring="Geeksforgeeks, is best @# Computer Science Portal.!!!"
res=re.findall(r'\w+',teststring)
print(res)
o/p: 74 days
CONVERTING STRING TO DATE
import datetime
def convert(date_time):
format = '%b %d %Y %I:%M%p' # The format
datetime_str = datetime.datetime.strptime(date_time, format)
return datetime_str
date_time = 'Dec 4 2018 10:07AM'
print(convert(date_time))
Comments
Post a Comment