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

在python中从字符串中提取未知子字符串,python,string,dictionary,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 我想提取位置之后

我有以下字符串:

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
我想提取
位置之后的内容:
直到新行

我不能在string方法中使用
子字符串,因为下面的“位置:`可能会改变”

我尝试将这个字符串放入字典,然后检索“LOCATION”键的值。但这似乎是在浪费内存和处理时间。因为这本字典对我来说,除了它的价值之外是无用的。此外,如果字符串太大,字典可能会变大


是否有其他方法提取“LOCATION:`直到'\n'?

您可以使用
拆分行
拆分行,然后根据
拆分单独的行,将它们转换为字典,如下所示

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"]
要将键转换为小写字母,可以像这样重构字典

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"]
字符串。索引(字符)是您需要的:

 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 ]

您可以使用正则表达式提取字符串

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

你可以在newline上拆分整个字符串。然后检查每一行是否开始 使用
位置
。如果它确实打印剩余的行

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

regex变量在这里是字符串吗?不是,但是
regex.group(1)
是字符串。你可能想将搜索和组提取分开,以便在不匹配的情况下测试
None
。是的,正如@Adam所说的
regex.group(1)
是字符串。你的答案救了我一天,感谢您可以使用正则表达式执行此任务。阅读我的更新,很抱歉我的第一个答案拼错了。这是不正确的。它会产生一个字符串“:要提取的字符串,后跟一个“.Adam:嗯,这正是字符串中的内容,请阅读问题!Downvoter,请让我知道如何改进此帖子。见鬼,我非常喜欢你的解决方案,因此删除了我的基于RE的解决方案。是否可以在不影响值的情况下将此词典的键小写?@sukhvir仅对键进行处理?@sukhvir请现在检查我的答案。原因如下:代码的零解释,在人们可能复制和粘贴的示例中隐藏
str
内置类型,硬编码值
10
,缩进错误。目前看来,@thefourtheye的解决方案要好得多,解决了一般情况。