Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 产品代码看起来像abcd2343,按字母和数字分割_Python_Split - Fatal编程技术网

Python 产品代码看起来像abcd2343,按字母和数字分割

Python 产品代码看起来像abcd2343,按字母和数字分割,python,split,Python,Split,我在一个文本文件中有一个产品代码列表,每个like上的产品代码如下所示: abcd2343 abw34324 abc3243-23A 因此它是字母,然后是数字和其他字符 我想在一个数字的第一次出现时拆分 In [32]: import re In [33]: s='abcd2343 abw34324 abc3243-23A' In [34]: re.split('(\d+)',s) Out[34]: ['abcd', '2343', ' abw', '34324', ' abc', '324

我在一个文本文件中有一个产品代码列表,每个like上的产品代码如下所示:

abcd2343 abw34324 abc3243-23A

因此它是字母,然后是数字其他字符

我想在一个数字的第一次出现时拆分

In [32]: import re

In [33]: s='abcd2343 abw34324 abc3243-23A'

In [34]: re.split('(\d+)',s)
Out[34]: ['abcd', '2343', ' abw', '34324', ' abc', '3243', '-', '23', 'A']
或者,如果要在第一次出现数字时分割:

In [43]: re.findall('\d*\D+',s)
Out[43]: ['abcd', '2343 abw', '34324 abc', '3243-', '23A']

  • \d+
    匹配一个或多个数字
  • \d*\d+
    匹配0个或多个数字,后跟1个或多个非数字
  • \d+\d+
    匹配一个或多个数字或一个或多个非数字
有关Python正则表达式语法的更多信息,请参阅


re.split(pat,s)
将使用
pat
作为分隔符拆分字符串
s
。如果
pat
以括号开始和结束(以便成为“捕获组”),则
re.split
也将返回与
pat
匹配的子字符串。例如,比较:

In [113]: re.split('\d+', s)
Out[113]: ['abcd', ' abw', ' abc', '-', 'A']   # <-- just the non-matching parts

In [114]: re.split('(\d+)', s)
Out[114]: ['abcd', '2343', ' abw', '34324', ' abc', '3243', '-', '23', 'A']  # <-- both the non-matching parts and the captured groups
因此,如果
s
以数字结尾,您可以通过使用
re.findall('\d+\d+',s)
而不是
re.split('(\d+),s)
避免以空字符串结尾:

重新导入
m=重新匹配(r“(?P[a-zA-Z]+)(?P+)$”,输入)
m、 组(“字母”)
m、 组(“其余部分”)
这涵盖了abc3243-23A的角盒,将为字母组输出
abc
,为
其余部分输出3243-23A


因为您说过它们都在单独的行上,所以您显然需要在
输入中一次放置一行,以便在第一个数字上进行分区

parts = re.split('(\d.*)','abcd2343')      # => ['abcd', '2343', '']
parts = re.split('(\d.*)','abc3243-23A')   # => ['abc', '3243-23A', '']
所以这两个部分总是第[0]部分和第[1]部分

当然,您可以将其应用于多个代码:

>>> s = "abcd2343 abw34324 abc3243-23A"
>>> results = [re.split('(\d.*)', pcode) for pcode in s.split(' ')]
>>> results
[['abcd', '2343', ''], ['abw', '34324', ''], ['abc', '3243-23A', '']]

如果每个代码都在单独的一行中,那么不要使用
s.split()
而使用
s.splitlines()

尝试此代码,它会很好地工作

import re
text = "MARIA APARECIDA 99223-2000 / 98450-8026"
parts = re.split(r' (?=\d)',text, 1)
print(parts)
输出:


['MARIA APARECIDA','99223-2000/98450-8026']

此函数还处理浮点数和负数

def separate_number_chars(s):
    res = re.split('([-+]?\d+\.\d+)|([-+]?\d+)', s.strip())
    res_f = [r.strip() for r in res if r is not None and r.strip() != '']
    return res_f
例如:

utils.separate_number_chars('-12.1grams')
> ['-12.1', 'grams']

如果使用“(\d+”,并且数字是字符串的最后一个字符,则列表中的最后一个条目将是空字符串。我们如何避免这种情况?IIUC,您可以使用
re.findall('(\d+\d+,'abcd2343 abw34324 abc3243-23')
,它返回
['abcd','2343','abw','34324','abc','3243','-','23']
>>> s = "abcd2343 abw34324 abc3243-23A"
>>> results = [re.split('(\d.*)', pcode) for pcode in s.split(' ')]
>>> results
[['abcd', '2343', ''], ['abw', '34324', ''], ['abc', '3243-23A', '']]
import re
text = "MARIA APARECIDA 99223-2000 / 98450-8026"
parts = re.split(r' (?=\d)',text, 1)
print(parts)
def separate_number_chars(s):
    res = re.split('([-+]?\d+\.\d+)|([-+]?\d+)', s.strip())
    res_f = [r.strip() for r in res if r is not None and r.strip() != '']
    return res_f
utils.separate_number_chars('-12.1grams')
> ['-12.1', 'grams']