Reputation: 3
Usually I use the .split() method to split strings on a dataframe, but this time I have a string that looks like this:
"3017381ª Série - EM"
or
"3017381º Ano - Iniciais"
And I'd like to find "º" or "ª" and then split it 2 characters before. The string should look like this after the split:
["301728","1ª Série - EM"]
Upvotes: 0
Views: 151
Reputation: 685
You could use the str.find function, and alter the location in the string you received.
in a function it would look like this (do consider it will split the string on the first entry of the options defined in charList, but you could extend the methodology if required for multiple splits):
def splitString(s,offset=0,charList=[]):
for opt in charList:
x = s.find(opt)
if x != -1: # default when not found
return [s[:x+offset],s[x+offset:]]
return [s] # input char not found
You can then call the function:
splitString("3017381ª Série - EM",offset=-1,charList=[ "º","ª"])
Upvotes: 2
Reputation: 46
Maybe it's a little bit confusing but the result it's the one you want.
a = "3017381ª Série - EM"
print(a[7])
print(a[5])
b = a[7]
c = a[5]
print(b)
print(c)
x = a.split(b)
y = a.split(c)
newY = y[1]
newX = x[0]
print(newY)
print(newX)
minusX = newX[:-1]
print(minusX)
z =[]
z.append(minusX)
z.append(y[1])
print("update:", z)
Upvotes: 0