Найти - Пользователи
Полная версия: sys.stdout
Начало » Python для новичков » sys.stdout
1
Mozart
Всем привет, нужно что бы при работе скрипта одну строку заменяла другая, а не так что бы они печатались последовательно вниз. Так вот почему вот так работает -

import sys, time

for i in range(1, 101, 5):
sys.stdout.write("\r ... %s%%" % i)
sys.stdout.flush()
time.sleep(0.05)

А так нет?

import sys, time

for i in open("input.txt", "r").readlines():
sys.stdout.write("\r ... %s" % i)
sys.stdout.flush()
time.sleep(0.05)

Нужно что бы работал именно второй вариант. Спасибо.
sp3
import sys, time
for i in open("input.txt", "r").readlines():
    sys.stdout.write("\r ... %s" % i.strip())
    sys.stdout.flush()
    time.sleep(0.05)
Mozart
Спасибо, в таком виде работает, но в классах все равно не хочет почему то … Ну да ладно, мб потом разберусь …
odnochlen
Mozart
А так нет?
Потому, что посмотри, что возвращает readlines(). Надо перенос строки откусывать.
Более того - readlines читает все в память.

import sys, time
for i in file("input.txt", "r"):
    print "\r ... %s%%" % i.rstrip('\r\n'),
    time.sleep(0.05)
reclosedev
Еще можно (если файл в память убирается)
for line in f.read().splitlines():
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