简体   繁体   English

从字符串中删除字符并创建子字符串的最pythonic方法是什么?

[英]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(1, 2)

SIC0575 length = 7, but it could dynamically change, so let's assume length = N SIC0575长度 = 7,但它可以动态变化,所以我们假设长度 = N

(1, 2) let's assume (0~9, 0~9) (1, 2)假设 (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.我不熟悉 Python,我想知道哪种方式是最优雅的/pythonic 方式来实现这一点。 Is there any built-in function to do that?有没有内置的 function 可以做到这一点?

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对于您的用例,我将 go 与index

Get the index of opening parenthesis ( , and take the first slice for aux , and use ast for val1 , val2获取左括号(index ,并取第一个切片为aux ,并使用astval1val2

import ast

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

OUTPUT : OUTPUT

print(aux, v1, v2)
SIC0575 1 2

I would go for something like below.我会 go 如下所示。

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)

暂无
暂无

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

相关问题 从字符串的开头删除数字的最Pythonic方法是什么? - What's the most Pythonic way to remove a number from start of a string? 从 Pandas 的列中的行中删除特殊字符的大多数 Pythonic 方法 - Most Pythonic way to remove special characters from rows in a column in Pandas 从字符串中删除多个子字符串的最有效方法? - Most efficient way to remove multiple substrings from string? 从枚举值创建 Literal 的最简单/最 Pythonic 的方法是什么? - What is the easiest / most pythonic way of create a Literal from an enum values? 在具有多种类型的空白字符的字符串中的每个单词上应用函数的最有效的方法是什么? - What's the most pythonic way to apply a function on every word in a string with multiple types of white space characters? 从字符串中删除尾随空格的pythonic方法是什么? - What is the pythonic way to remove trailing spaces from a string? 组合多个字符串列以创建新的 Pandas 系列的最pythonic 方法是什么? - What is the most pythonic way to combine multiple string columns to create a new Pandas series? 从具有不同长度的字符串的二维列表创建数据帧的最 Pythonic/时尚/有效的方法 - Most pythonic/stylish/efficient way to create a dataframe from 2-dimensional list of string with varied length 创建一个通过字典过滤的“For Loop”的最pythonic方法是什么? - What is the most pythonic way to create a 'For Loop' that filters through a dictionary? 什么是避免在字符串中指定相同值的最pythonic方法 - What is the most pythonic way to avoid specifying the same value in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM