Форум сайта python.su
Нужно написать код Пайтон 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
Офлайн
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_))
Отредактировано AD0DE412 (Фев. 6, 2022 20:30:54)
Офлайн
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() + ' ')
Офлайн
>>> 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 >>>
Отредактировано py.user.next (Фев. 7, 2022 01:34:40)
Офлайн