Найти - Пользователи
Полная версия: Удаление новых строк в опред. позициях(чтоб в одной строке инфа про одного человека)
Начало » Python для новичков » Удаление новых строк в опред. позициях(чтоб в одной строке инфа про одного человека)
1
Maugli
Нужно написать код Пайтон 3, чтоб каждого в одну строку, убрать так сказать ненужные новые строки:::
Мой неработающий код:
file = open('текстовыйфайл', ‘r’)
c = file.readlines()
for line in c:
print(''.join(line.strip()))

Сам список еще длинный тут для примера взял несколько
Нужно чтоб из этого :
12:01:0827635 Настя
20.00.0000 ifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Лера
20.00.0000 jifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Дима
20.00.0000 jifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Саня
20.00.0000 ifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf

выводило этооо::: инфа про каждого в одну строку
12:01:0827635 Настя 20.00.0000 ifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Лера 20.00.0000 jifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Дима 20.00.0000 jifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Саня 20.00.0000 ifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
AD0DE412
 txt_file__emulate = """12:01:0827635 Настя
20.00.0000 ifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Лера
20.00.0000 jifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Дима
20.00.0000 jifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Саня
20.00.0000 ifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf""".split("\n")
sum_ = ""
end_ = []
for i , j in enumerate(txt_file__emulate, 1):
    if i % 3:
        sum_ += j + " "
    else:
        sum_ += j
        end_.append(sum_)
        sum_ = ""
    
print("\n".join(end_))
xam1816
 with open('text.txt','r') as r_file:
    with open('out.txt', 'w') as w_file:
        for n, i in enumerate(r_file):
            if n % 3 == 2:
                w_file.write(i)
            else:
                w_file.write(i.strip() + ' ')
py.user.next
  
>>> import io
>>> 
>>> text = """\
... 12:01:0827635 Настя
... 20.00.0000 ifggggggg ggggggggggg
... gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
... 12:01:0827635 Лера
... 20.00.0000 jifggggggg ggggggggggg
... gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
... 12:01:0827635 Дима
... 20.00.0000 jifggggggg ggggggggggg
... gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
... 12:01:0827635 Саня
... 20.00.0000 ifggggggg ggggggggggg
... gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
... """
>>> 
>>> stream = io.StringIO(text)
>>> 
>>> def translate(strings):
...     n = 0
...     for i in strings:
...         if n == 0:
...             n += 1
...             yield i[:-1]
...         elif n == 1:
...             n += 1
...             yield ' ' + i[:-1]
...         elif n == 2:
...             n = 0
...             yield ' ' + i
... 
>>> for i in stream:
...     print(i, end='')
... 
12:01:0827635 Настя
20.00.0000 ifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Лера
20.00.0000 jifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Дима
20.00.0000 jifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Саня
20.00.0000 ifggggggg ggggggggggg
gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
>>> _ = stream.seek(0)    
>>> 
>>> for i in translate(stream):
...     print(i, end='')
... 
12:01:0827635 Настя 20.00.0000 ifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Лера 20.00.0000 jifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Дима 20.00.0000 jifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
12:01:0827635 Саня 20.00.0000 ifggggggg ggggggggggg gggggggggggggggg ggggggggggggggggggg ggggggggggggggggg ggggggggggggggggggggggggg gfgfgfgfgfgf
>>>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB