-3

I have string trying to get last word from string like this AUTHORITY["EPSG","6737"]

a = 'PROJCS["Korea",GEOGCS["K 2000",DATUM["Geocentric_datum",
SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],
AUTHORITY["EPSG","6737"]]]'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Vas
  • 395
  • 1
  • 3
  • 13
  • 1
    Does this answer your question? [Split a string by a delimiter in python](https://stackoverflow.com/questions/3475251/split-a-string-by-a-delimiter-in-python) – Tomerikoo Mar 20 '23 at 09:41

3 Answers3

-1

We can solve this problem using regular expressions

import re

a = 'PROJCS["Korea",GEOGCS["K 2000",DATUM["Geocentric_datum",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6737"]]]'

matches = re.findall('[A-Z]+\["[A-Za-z]+","\d+"\]', a)

# prints the last match
print(matches[-1])

Here's a breakdown of the regular expression:

[A-Z]+ matches the first word in all caps. "AUTHORITY"

\[ matches the opening bracket [ is a special character in regex so we have to make sure we escape it using \

"[A-Za-z]+" will match word in quotes.

, matches the comma

"\d+" matches the number in quotes

\] matches the closing bracket

alieb
  • 85
  • 4
-1

a = 'PROJCS["Korea",GEOGCS["K 2000",DATUM["Geocentric_datum",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6737"]]]'

split_list = a.split('],') your_word = split_list[-1]

print(your_word[0:-2])

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 25 '23 at 01:38
-1

That depends on what You think is a "word" in this case? You mean the mentioned phrase AUTHORITY["EPSG","6737"] ? You can always use split function that returns an array of strings upon taking a delimiter ("," in this example): splitString = someString.split(","). This way you can access a specific part of initial string by using some index i later on: word = splitString[i]. In Your case just take index = -2 and -1 ( meaning second and first from END respectively) and combine them with simple string operation:

splitString = someString.split(",")
output = splitString[-2] + "," + splitString[-1]
output.replace(']]]',']') 

Last line of snippet removes unnecessary "]]" at the end of our string
BUT: In your case it can be clearly seen that this is some datastructure parsed into a complex array form. I would suggest parsing that string into a datastructure and then You have a clear way to access particular parts of data. Where do You get it from? You can try parsing it into json object using some python libraries. That would probably be best if some further operations on strings like this one are needed. If You just need a simple solution for one time, the code above can do the job.