[英]Get string inbetween text Python
If I have: 如果我有:
127.0.0.1 - - [24/Feb/2014:03:36:46 +0100] "POST /info HTTP/1.1" 302 0 "http://website.com" "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36" "name=josh&zipcode=12345"
How would I be able to extract "josh" and "12345" to their own variables? 我如何才能将“ josh”和“ 12345”提取到它们自己的变量中?
Split the string by spaces, take the last element, strip quotes and use urlparse.parse_qsl() to parse query parameters: 用空格分隔字符串,使用最后一个元素, 去除引号,然后使用urlparse.parse_qsl()解析查询参数:
>>> from urlparse import parse_qsl
>>> s = '127.0.0.1 - - [24/Feb/2014:03:36:46 +0100] "POST /info HTTP/1.1" 302 0 "http://website.com" "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36" "name=josh&zipcode=12345"'
>>> params = parse_qsl(s.split()[-1].strip('"'))
>>> params
[('name', 'josh'), ('zipcode', '12345')]
Then, to assign variables to the parameter values, you can unzip params
: 然后,要将变量分配给参数值,可以解压缩params
:
>>> name, zipcode = zip(*params)[1]
>>> name
'josh'
>>> zipcode
'12345'
You can use this following code, assuming all of the following strings will be in the same format: 您可以使用以下代码,假设以下所有字符串的格式相同:
>>> info = '127.0.0.1 - - [24/Feb/2014:03:36:46 +0100] "POST /info HTTP/1.1" 302 0 "http://website.com" "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36" "name=josh&zipcode=12345"'
>>> name = info.split()[-1].split('&')[0].split('=')[1]
>>> code = info.split()[-1].split('&')[1].split('=')[1]
>>> name
'josh'
>>> code
'12345'
The first .split()
is to get the entire string as a list. 第一个.split()
是将整个字符串作为列表获取。
The [-1]
is to get the last item in the list. [-1]
用于获取列表中的最后一项。
The .split('&')
is to split the last sequence by the '&'
. .split('&')
将最后一个序列除以'&'
。
The [0]
or [1]
is to specify which value we want to obtain, the name or the code. [0]
或[1]
用于指定我们要获取的值,名称或代码。
The split('=')
is to split each value by the equals sign, so that we can obtain the name or the code as one value. split('=')
是用等号将每个值分开,以便我们可以将名称或代码作为一个值获得。
The last [1]
is to get the last value, basically to exclude the 'name'
or 'zipcode'
. last [1]
是获取最后一个值,基本上是排除'name'
或'zipcode'
。
You could use the split function... 您可以使用分割功能...
o = "name=josh&zipcode=12345"
a = o.split('&') # ['name=josh', 'zip=12345']
d = dict(s.split('=') for s in a)
would give you a nice dictionary of key value pairs :) 会给你一个很好的键值对字典:)
{'name':'josh','zip':12345}
or you could use something else depending on what you need... http://docs.python.org/2/library/string.html 或者您可以根据需要使用其他东西... http://docs.python.org/2/library/string.html
string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.