frazman
frazman

Reputation: 33243

Appending various strings between two delimeters in one string. python

I've been stuck here a day. So I thought to ask the experts. I am reading contents from a file which has data in form

|something|
|something else|
|something_1
 something_2
 someting_3|
|something blah blah|

.. and so on.. so as you guys figured it out.. the delimiter is '|' now.. I want the output in following form |something| |something else| |something_1_something_2_someting_3| |something blah blah| basically everything between a delimiter in one string Any clues how to go about it Programming language is Python

Upvotes: 0

Views: 128

Answers (2)

Andrew Martin
Andrew Martin

Reputation: 86

I think you're making this too complicated for yourself. The way you've described your output format, it seems like all you really need to do is put everything on one line. If I'm right about this, you can read all of the data in as a string (as long as the file isn't too large), remove newline characters ('\n' or '\r\n' depending on your system) and write this out to a file. Alternatively, if you want to be more memory efficient you can iterate through the file line by line (my recommended solution).

If I was mistaken about your output format, comment here and I'll help you out with that. I didn't provide any code because this is something you can easily find in the Python documentation (i.e. string methods, file I/O).

Upvotes: 0

Winston Ewert
Winston Ewert

Reputation: 45039

import re
print re.findall(r"\|[^|]*\|", text)

If you haven't seen this before, its a regular expression. Basically you describe a pattern in text that you are looking for. I recommend reading up on it if you don't know them.

Upvotes: 1

Related Questions