从python中的字符串中提取未知子字符串

时间:2014-06-05 06:37:42

标签: python string dictionary

我有以下字符串:

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

我想提取LOCATION:之后的任何内容直到新行。

我无法执行substring in string方法,如下所示' LOCATION:`可能会改变。

我尝试将此字符串放入字典中,然后检索' LOCATION'键。但这似乎浪费了内存和处理时间。因为除了这个价值之外,字典对我来说毫无用处。如果字符串太大,字典也可能会增加geatlt

有没有其他方法可以提取以下内容:“位置:”直到' \ n' ??

4 个答案:

答案 0 :(得分:3)

使用正则表达式

提取字符串
>>> 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

答案 1 :(得分:2)

您可以使用splitlines拆分行,然后根据:拆分各行,将它们转换为字典,就像这样

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

要将键转换为小写字母,您可以像这样重建字典

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"]

答案 2 :(得分:2)

string.index(character)就是你所需要的:

 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 ]

答案 3 :(得分:2)

您可以在换行符上拆分整个字符串。然后检查每一行是否开始 与LOCATION。如果它确实打印了剩余的行。

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