quesadyllan quesadyllan - 1 month ago 18
Python Question

Explanation for re.split() error?

I'm sure you see this a lot but just wanted to warn you that I'm still very new to coding in general and have a lot to learn. I've been attempting teaching myself by working on a personal project and youtube whenever I have the time.

Long version: I always get an error when I use the split function, but I still get a value. Essentially what my function is supposed to do is take an angle or bearing that is in degrees┬░minutes'seconds" or for example "N 38┬░43'23\" E" to a decimal number. I took advantage of bearings being less than 90 degrees to use indexing to find the numbers in the string to convert the angle. However, if the angle wasn't a bearing, I couldn't think of a way to figure out how many tens places the degrees to pull the number out so I tried splitting it at the degree symbol. When it does this, though, it returns an error saying split() requires a non-empty pattern match. What does this mean? The code still works, but always returns this error with it.

Short version: Here's my code:

def decimal(x): # converts degrees-minutes-seconds to decimal
dms = list(x)
if dms[0] == 'N' or dms[0] == 'S':
degrees = ((int(dms[2]) * 10) + int(dms[3]))
minutes = ((int(dms[-8]) * 10) + int(dms[-7])) / 60
seconds = ((int(dms[-5]) * 10) + int(dms[-4])) / 3600
else:
placeholder = re.split(r'\u00b0*', x)
degrees = int(placeholder[0])
minutes = ((int(dms[-6]) * 10) + int(dms[-5])) / 60
seconds = ((int(dms[-3]) * 10) + int(dms[-2])) / 3600
return degrees + minutes + seconds


When I run it, I get the decimal number, but I also get:

FutureWarning: split() requires a non-empty pattern match.
return _compile(pattern, flags).split(string, maxsplit)


I've looked at other questions posted about this error, but there were no answers that explained what to do to avoid this, or what the error really means. Thank you for any help. Also any advice or corrections you see would be greatly appreciated!

Answer

What you have is not an error, it's a warning.

For one, I think you should not use a raw string, you need '\u00b0', the character. And 'c*' will match 0 or more of c, so it matches the empty string. I believe this is what the warning is about. I suggest that you use

re.split('\u00b0', x)

Or even better, just

 x.split('\u00b0')