简体   繁体   English

根据字符位置从字符串中提取子字符串-Python

[英]Extract a substring from a string based on the position of character - Python

Im trying to extract the substrings from the below string 我试图从下面的字符串中提取子字符串

   package: name='com.example.tracker' versionCode='1' versionName='1.0'

as string 1 : versionCode='1' and as string 2: versionName='1.0' 作为字符串1:versionCode ='1'和作为字符串2:versionName ='1.0'

I used str.find('versionCode) which returns me the index of 'v' in the versioncode and i used string length to access '1'. 我使用了str.find('versionCode),它向我返回了版本代码中的'v'索引,并且我使用了字符串长度来访问'1'。 However there are time the versioncode might be a double digit number so I can't fix the location of the digit. 但是,有时版本代码可能是两位数字,所以我无法确定数字的位置。 Is there a way to achieve this? 有没有办法做到这一点?

If the string is 如果字符串是

    package: name='com.example.tracker' versionCode='12' versionName='12.0'

I need to extract 12 and 12.0. 我需要提取12和12.0。 My implementation can support single digits but the digits will vary. 我的实现可以支持个位数,但是位数会有所不同。

 if line.find('versionCode') != -1:
            x = line.find('versionCode') 
            versionCode = line[x+13:x+15] 

You'll need to use regular expressions to do this. 您将需要使用正则表达式来执行此操作。

In each of the below lines we use the pattern (.*?) to perform a non-greedy search within the quotes to extract the string, and then pull group(1) rather than group(0) on the returned object, as 0 returns the full match across the whole input string, and 1 gives the first regex capture group. 在下面的每一行中,我们使用模式(.*?)在引号内执行非贪婪搜索以提取字符串,然后在返回的对象上将group(1)而不是group(0)拉为0返回整个输入字符串的完全匹配,并且1给出第一个正则表达式捕获组。

import re

packageDetails = "package: name='com.example.tracker' versionCode='1' versionName='1.0'"
name = re.search("name='(.*?)'", packageDetails).group(1)
versionCode = re.search("versionCode='(.*?)'", packageDetails).group(1)
versionName = re.search("versionName='(.*?)'", packageDetails).group(1)

print "package name is :", name
print "version code is :", versionCode
print "version name is :", versionName 

And this outputs: 并输出:

package name is : com.example.tracker
version code is : 1
version name is : 1.0

You could manipulate the string with built-in methods to get the values you need: 您可以使用内置方法来操作字符串以获取所需的值:

packageDetails = "package: name='com.example.tracker' versionCode='1' versionName='1.0'"
details = packageDetails
params = ['name=', 'versionCode=', 'versionName=']
params.reverse()
values = []
for p in params:
    details, v = details.split(p)
    values.append(v.strip().strip("'"))
values.reverse()

Or you could build a dictionary: 或者您可以构建一个字典:

>>> details = { x.split('=')[0] : x.split('=')[1].strip("'") for x in a.split()[1:] }
>>> details
{
  "name" : "com.example.tracker",
  "versionCode" : "1",
  "versionName" : "1.0"
}
>>> details['name']
"com.example.tracker"
>>> details['versionCode'] == '1'
true

Or if you don't care about stripping the "'"s 或者,如果您不关心剥离“'”

>>> dict(x.split('=') for x in a.split()[1:])
{
  "name" : "'com.example.tracker'",
  "versionCode" : "'1'",
  "versionName" : "'1.0'"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM