开发者

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


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜