Python正则表达式根据数字后面的逗号进行拆分
我有一个大文件,需要从中加载到字符串列表中。每个元素将包含文本,直到紧跟在数字后面的“,”为止 例如:Python正则表达式根据数字后面的逗号进行拆分,python,regex,string,Python,Regex,String,我有一个大文件,需要从中加载到字符串列表中。每个元素将包含文本,直到紧跟在数字后面的“,”为止 例如: this is some text, value 45789, followed by, 1245, and more text 78965, more random text 5252, 这应成为: ["this is some text, value 45789", "followed by, 1245", "and more text 78965", "more random text
this is some text, value 45789, followed by, 1245, and more text 78965, more random text 5252,
这应成为:
["this is some text, value 45789", "followed by, 1245", "and more text 78965", "more random text 5252"]
我目前正在做re.sub(r'([0-9]+),,,,,,,)
然后在“~”上拆分(因为我的文件不包含~),但这会在逗号前抛出数字。。有什么想法吗?您可以使用:
>>重新导入
>>>
>>>text='这是一些文本,值45789,后跟,1245,更多文本78965,更多随机文本5252,'
>>>re.split(r’(?如果希望它也处理空格,请执行以下操作:
string = " blah, lots , of , spaces, here "
pattern = re.compile("^\s+|\s*,\s*|\s+$")
result = [x for x in pattern.split(string) if x]
print(result)
>>> ['blah', 'lots', 'of', 'spaces', 'here']
re.split(')(?
string = " blah, lots , of , spaces, here "
pattern = re.compile("^\s+|\s*,\s*|\s+$")
result = [x for x in pattern.split(string) if x]
print(result)
>>> ['blah', 'lots', 'of', 'spaces', 'here']