Python split question.

Asked 2 years ago, Updated 2 years ago, 103 views

import re

text = 'tls_0925_13")'

pattern = re.compile(r'\w\d+"')
s = str(re.findall(pattern, text))

print(s) #Output ['_13"]

k = text.split(s)

print(k) #Output ['tls_0925_13");']

tls_0925 I wanted to extract this part, so I practiced the above part I can't split it. Is there a way?

python split

2022-09-20 11:07

2 Answers

>>> text = 'tls_0925_13")'
>>> text2 = '_'.join(text.split('_')[:2])
>>> text2
'tls_0925'


2022-09-20 11:07

If you want to write str objects in the list, you must join or use the list index.

s = ''.join(re.findall(pattern, text))
Or
s = re.findall(pattern, text)[0]
k = text.split(s)


2022-09-20 11:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.