Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python split() without removing the delimiter [duplicate]

This code almost does what I need it to..

for line in all_lines:
    s = line.split('>')

Except it removes all the '>' delimiters.

So,

<html><head>

Turns into

['<html','<head']

Is there a way to use the split() method but keep the delimiter, instead of removing it?

With these results..

['<html>','<head>']
like image 224
some1 Avatar asked Oct 23 '11 12:10

some1


People also ask

What is split () in Python?

Definition and Usage. The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Can Split have multiple separators Python?

To split a string with multiple delimiters in Python, use the re. split() method. The re. split() function splits the string by each occurrence of the pattern.

Can I split a string by two delimiters Python?

Python has a built-in method you can apply to string, called . split() , which allows you to split a string by a certain delimiter.


4 Answers

d = ">"
for line in all_lines:
    s =  [e+d for e in line.split(d) if e]
like image 126
P.Melch Avatar answered Oct 17 '22 15:10

P.Melch


If you are parsing HTML with splits, you are most likely doing it wrong, except if you are writing a one-shot script aimed at a fixed and secure content file. If it is supposed to work on any HTML input, how will you handle something like <a title='growth > 8%' href='#something'>?

Anyway, the following works for me:

>>> import re
>>> re.split('(<[^>]*>)', '<body><table><tr><td>')[1::2]
['<body>', '<table>', '<tr>', '<td>']
like image 36
gb. Avatar answered Oct 17 '22 14:10

gb.


How about this:

import re
s = '<html><head>'
re.findall('[^>]+>', s)
like image 21
Óscar López Avatar answered Oct 17 '22 15:10

Óscar López


Just split it, then for each element in the array/list (apart from the last one) add a trailing ">" to it.

like image 1
orangething Avatar answered Oct 17 '22 14:10

orangething