splitting by double newline

N

Nikola Skoric

Hello everybody,

I'd like to split a file by double newlines, but portably. Now,
splitting by one or more newlines is relatively easy:

self.tables = re.split("[\r\n]+", bulk)

But, how can I split on double newlines? I tried several approaches,
but none worked...
 
I

Ian

Hello everybody,

I'd like to split a file by double newlines, but portably. Now,
splitting by one or more newlines is relatively easy:

self.tables = re.split("[\r\n]+", bulk)

But, how can I split on double newlines? I tried several approaches,
but none worked...

self.tables = re.split(r'(?:\r\n){2,}|\r{2,}|\n{2,}', bulk)
 
P

Peter Otten

Nikola said:
Hello everybody,

I'd like to split a file by double newlines, but portably. Now,
splitting by one or more newlines is relatively easy:

self.tables = re.split("[\r\n]+", bulk)

But, how can I split on double newlines? I tried several approaches,
but none worked...

If you open the file in universal newline mode with

with open(filename, "U") as f:
bulk = f.read()

your data will only contain "\n". You can then split with

blocks = bulk.split("\n\n") # exactly one empty line

or

blocks = re.compile(r"\n{2,}").split(bulk) # one or more empty lines

One last variant that doesn't read in the whole file and accepts lines with
only whitespace as empty:

with open(filename, "U") as f:
blocks = ("".join(group) for empty, group in itertools.groupby(f,
key=str.isspace) if not empty)
 
N

Nikola Skoric

Dana Mon, 7 Feb 2011 10:02:05 -0800 (PST),
Ian said:
self.tables = re.split(r'(?:\r\n){2,}|\r{2,}|\n{2,}', bulk)

Thanks!

I tried without "?:", but it didn't work. Can you tell me why is it
important that group is noncapturing?
 
M

MRAB

Dana Mon, 7 Feb 2011 10:02:05 -0800 (PST),


Thanks!

I tried without "?:", but it didn't work. Can you tell me why is it
important that group is noncapturing?
The scanner uses capture groups to identify which part matched. If you
provide additional capture groups it has problems...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top