2

I have the following string:

HTTP/1.1 200 OK
CACHE-CONTROL: max-age=100
EXT:
LOCATION: string to be extracted followed by a \n
SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1
ST: urn:schemas-upnp-org:device:basic:1
USN: uuid:2f402f80-da50-11e1-9b23-0017881892ca

I want to extract whatever follows LOCATION: until the new line.

I can't do substring in string method as whatever follows 'LOCATION: ` may change.

I tried making this string into a dictionary and then retrieve the value of 'LOCATION' key. But this seems like a waste of memory and processing time. As the dictionary is useless to me apart from that value. Also the dictionary may grow geatlt in size if the string is too large

Is there any other way to extract whatever follows 'LOCATION: ` until the '\n' ??

sukhvir
  • 5,265
  • 6
  • 41
  • 43

4 Answers4

4

You extract the string using regular expressions

>>> import re
>>> string = """HTTP/1.1 200 OK
... CACHE-CONTROL: max-age=100
... EXT:
... LOCATION: http://129.94.5.95:80/description.xml
... SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1
... ST: urn:schemas-upnp-org:device:basic:1
... USN: uuid:2f402f80-da50-11e1-9b23-0017881892ca
... """
>>> regex = re.compile('LOCATION: (.*?)\n')
>>> m = regex.search(string)
>>> if m:
...     print m.group(1)
http://129.94.5.95:80/description.xml
Sayan Chowdhury
  • 419
  • 4
  • 13
2

You can split the lines with splitlines and then split the individual lines based on :, to convert them to a dictionary, like this

d = dict(item.split(": ", 1) for item in data.splitlines() if ": " in item)
print d["LOCATION"]
# http://129.94.5.95:80/description.xml

To have the keys converted to lowercase letters, you can reconstruct the dictionary like this

d = dict(item.split(": ", 1) for item in data.splitlines() if ": " in item)
d = {key.lower():d[key] for key in d}
print d["location"]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
2

string.index(character) is what you need :

 mystr="HTTP/1.1 200 OK\nCACHE-CONTROL: max-age=100\nEXT:\nLOCATION: string to be extracted followed by a \nSERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\nST: urn:schemas-upnp-org:device:basic:1\nUSN: uuid:2f402f80-da50-11e1-9b23-0017881892ca"
search = "LOCATION"
start = mystr.index(search)+len(search)
stop = mystr.index("\n", start)
print mystr [ start : stop ]
Louis
  • 2,854
  • 2
  • 19
  • 24
2

You could split the whole string on newline. And then check each line if they start with LOCATION. If it does print the remaining line.

string = """HTTP/1.1 200 OK
CACHE-CONTROL: max-age=100
EXT:
LOCATION: http://129.94.5.95:80/description.xml
SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1
ST: urn:schemas-upnp-org:device:basic:1
USN: uuid:2f402f80-da50-11e1-9b23-0017881892ca"""



 for line in string.split('\n'):
     if line.startswith('LOCATION'):
         print(line[10:])
         break

Out: http://129.94.5.95:80/description.xml
Ankur Ankan
  • 2,953
  • 2
  • 23
  • 38
  • 1
    Several reasons: *Zero* explanation for the code, shadowing the `str` builtin type in an example that people might copy and paste, hard-coding the value `10`, bad indentation. As it stands, @thefourtheye's solution is much better and solves the general case. – Lukas Graf Jun 05 '14 at 07:11