3

I got an file with text form:

a:
b(0.1),
c(0.33),
d:
e(0.21),
f(0.41),
g(0.5),
k(0.8),
h:
y(0.9),

And I want get the following form:

a: b(0.1), c(0.33)
d: e(0.21), f(0.41), g(0.5), k(0.8)
h: y(0.9)

In python language, I have tried:

for line in menu:
    for i in line:
        if i == ":":

but i do not know if i wondering print (text before i and after i till meet another i) as one line.

also delete the ',' at end of the line

Sorin
  • 11,863
  • 22
  • 26
ArrayNo1
  • 33
  • 7
  • 1
    possible duplicate of [Print in one line dynamically](http://stackoverflow.com/questions/3249524/print-in-one-line-dynamically) – Tim Sep 23 '15 at 08:26
  • this answer is working:one_line = ''.join(menu).replace('\n', ' ') list=one_line.split(',') s = ''; for x in list: if ':' in x: x='\n'+ x s = s + x +' ' print(s) – ArrayNo1 Sep 24 '15 at 00:35

3 Answers3

3
import re

one_line = ''.join(menu).replace('\n', ' ')
print re.sub(', ([a-z]+:)', r'\n\1', one_line)[:-1]

You might have to tweak the one_line to match your input better.

Sorin
  • 11,863
  • 22
  • 26
  • it seems print in one line, and have not remove ',' when i am running in InteljIDEA – ArrayNo1 Sep 23 '15 at 10:01
  • @ArrayNo1 new line is system specific. Try replacing `\n` in the last line with either `\r` or `\n\r` or `os.linesep + r'\1'` (you'll need to import os for the last one). – Sorin Sep 23 '15 at 10:02
  • I got an answer it is working: one_line = ''.join(menu).replace('\n', ' ') list=one_line.split(',') s = ''; for x in list: if ':' in x: x='\n'+ x s = s + x +' ' print(s) – ArrayNo1 Sep 24 '15 at 00:35
1

I am not exactly sure if you want to print the stuff or actually manipulate the file. But in the case of just printing:

from __future__ import print_function
from itertools import tee, islice, chain, izip

def previous_and_next(some_iterable):
    prevs, items, nexts = tee(some_iterable, 3)
    prevs = chain([None], prevs)
    nexts = chain(islice(nexts, 1, None), [None])
    return izip(prevs, items, nexts)

with open('test.txt') as f:
    for i, (prev, line, next) in enumerate(previous_and_next(f)):
        if ":" in line and i != 0: #Add a newline before every ":" except the first.
            print()
        if not next or ":" in next: #Check if the next line is a semicolon, if so strip it. "not next" is need because if there is no next NoneType is returned.
            print(line.rstrip()[:-1], end=" ")
        else: #Otherwise just strip the newline and don't print any newline.
            print(line.rstrip(), end=" ")

Using this helper function.

Community
  • 1
  • 1
Dair
  • 15,910
  • 9
  • 62
  • 107
1

Here a solution using an OrderedDict to store ':'-containing lines as key and the following lines as value until the next key is found. Then just print the dictionary as you like.

from collections import OrderedDict

data = OrderedDict()
key = False
for line in menu:
    if ':' in line:
        key = line.strip()
        data.update({line:[]})
    else:
        if key:
            data[key].append(line.strip(','))


for key in data:
    print(key,end=' ')

    if data[key][-1] != '':
        for item in data[key][:-1]:
            print(item, end=', ')
        print(data[key][-1])
    else:
        print(data[key][0])

Gives:

a: b(0.1), c(0.33)
d: e(0.21), f(0.41), g(0.5), k(0.8)
h: y(0.9)
alec_djinn
  • 10,104
  • 8
  • 46
  • 71