0

I have such strings:

ye<V><vn:inf><N><0><V><cpl:pres><3s>
çok<Postp:adv:ablC><0><N><0><V><cpl:pres><3s>
yağ<N><li><Adv><0><N><0><V><cpl:evid><3s>

And I want to extract;

ye, V, 3s
çok, Postp:adv:ablC, 3s
yağ, N, 3s

I have hundreds millions of such strings. What can be the best, efficient, and fastest way to do it? Can you show an example?

Thanks,

1
  • 2
    Where is your code? Commented Dec 1, 2016 at 12:10

3 Answers 3

5

Try this:

l = s.split('<')
'{}, {}, {}'.format(l[0], l[1][:-1], l[-1][:-1])

Example of output:

>>> s = 'ye<V><vn:inf><N><0><V><cpl:pres><3s>'
>>> l = s.split('<')
>>> '{}, {}, {}'.format(l[0], l[1][:-1], l[-1][:-1])
'ye, V, 3s'
Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest using format instead of string concatenation with +
2

You could try using using the findall. For example,

import re
regex = re.compile(r'(?P<g1>3s)|(?P<g2>ye)')
regex.findall(test_string)

This will return a list of tuples for the matches like the following:

# Output
# [('3s', ''), ('', 'ye'), ('3s', ''), ('', 'ye')]    

The regular expression that I compiled does not have all of the named groups that you desire, but you can add those easily enough.

Comments

1
s1 = 'ye<V><vn:inf><N><0><V><cpl:pres><3s>'
s2 = 'çok<Postp:adv:ablC><0><N><0><V><cpl:pres><3s>'
s3 = 'yağ<N><li><Adv><0><N><0><V><cpl:evid><3s>'

if __name__ == '__main__':
    for s in (s1,s2,s3):
        print('{0}, {1}, {2}'.format(s.split('<')[0], s.split('<')[1].split('>')[0], s.split('<')[-1].split('>')[0]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.