简体   繁体   中英

What is the most pythonic way to remove characters from a string and create substrings?

Let's say we have this string: SIC0575(1, 2)

SIC0575 length = 7, but it could dynamically change, so let's assume length = N

(1, 2) let's assume (0~9, 0~9)

I would like to create substrings from this string.

Desired result:

val1 = SIC0575

val2 = 1

val3 = 2

I am not familiar with Python, I would like to know which is the most elegant/pythonic way to achieve that. Is there any built-in function to do that?

What I've tried (it works, but it's not elegant at all):

        data = "SIC0575(1, 2)"
        aux = data.split("(")
        val1 = aux[0]
        aux2 = aux[1].split(",")
        val2 = aux2[0]
        val3 = aux2[1][:-1]

regexes are your friend:

import re
s = "SIC0575(1, 2)"
val1, val2, val3 = re.fullmatch(r"(.*?)\((\d), (\d)\)", s).groups()

For your use case, I'd go with index

Get the index of opening parenthesis ( , and take the first slice for aux , and use ast for val1 , val2

import ast

data = "SIC0575(1, 2)"
index = data.find('(')
aux = data[:index]
val1, val2 = ast.literal_eval(data[index:])

OUTPUT :

print(aux, v1, v2)
SIC0575 1 2

I would go for something like below.

import re
stringEx = [_.replace(' ','') for _ in re.split('\)|\(|,',"SIC0575(1, 2)")[:-1]]
var1,var2,var3 = string[0],int(stringEx[1]),int(stringEx[2])

out

val1 = SIC0575

val2 = 1

val3 = 2

You can use a regular expression to match the strings.

import re

data = "SIC0575(1, 2)"
pattern = re.compile(r'(.*)\(([0-9]), ([0-9])\)')
val1, val2, val3 = pattern.match(data).groups()
print(val1, val2, val3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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