Найти - Пользователи
Полная версия: printf выводит лишний перевод строки
Начало » Python для новичков » printf выводит лишний перевод строки
1 2 3
Shaman
mbele-mbele
Как бы их устранить.
for line in proc_list.stdout:
    print line.rstrip()
mbele-mbele
Shaman
СПАСИБО!
Shaman
Ого! Пожалуйста! )
J.R.
.
Shaman
Всё же переводить строки лучше поручить системе.
mbele-mbele
J.R.
Да тоже работает спасибо, занесу в копилку.
))
mbele-mbele
ПОзвольте еще маленький вопрос
# -*- coding: utf-8 -*-
import subprocess
import os
os.system("cls")
proc_list = subprocess.Popen('tasklist', shell=True, stdout=subprocess.PIPE)
lines = []
for line in proc_list.stdout:
    if 'PID' in line:
        utext = unicode(line.rstrip(), 'cp1251', 'replace')
        print utext.rstrip()
    if '========' in line:
        print line.rstrip()
    if 'firefox.exe' in line:
        lines.append(line)
        print line.rstrip()
exit()

… выводит
C:\Python27\python.exe C:/001/test12.py
€¬п ®Ўа §  PID €¬п бҐббЁЁ ь ᥠ­б  Џ ¬пвм
========================= ======== ================ =========== ============
firefox.exe 5664 Console 17 428я232 ЉЃ

Process finished with exit code 0

Вопрос как сделать чтобы шапочка из русских букв была “нормальными ” буквами?
utext = unicode(line.rstrip(), ‘cp1251’, ‘replace’) - не работает (((
Спасибо.

Shaman
# -*- coding: utf-8 -*-
import sys
reload(sys)
import locale
sys.setdefaultencoding(locale.getpreferredencoding())
 
import subprocess
import os
os.system("cls")
proc_list = subprocess.Popen('tasklist', shell=True, stdout=subprocess.PIPE)
lines = []
for line in proc_list.stdout:
    if 'PID' in line:
        utext = unicode(line.rstrip(), 'cp1251', 'replace')
        print utext.rstrip()
    if '========' in line:
        print line.rstrip()
    if 'firefox.exe' in line:
        lines.append(line)
        print line.rstrip()
exit()
Только для второго питона.
mbele-mbele
Shaman
Странно , но у меня
win8.1x64
Python 2.7

но даже с Вашими вставками “крючки” не пропали
C:\Python27\python.exe C:/001/test12.py
€¬п ®Ўа §  PID €¬п бҐббЁЁ ь ᥠ­б  Џ ¬пвм
========================= ======== ================ =========== ============
firefox.exe 5664 Console 17 517я148 ЉЃ

Process finished with exit code 0

… а у Вас русские в шапочке?
mbele-mbele
“Сверх” задача у меня - совместить вывод двух программ.
Вся программа выглядит так
# -*- coding: utf-8 -*-
import subprocess
import re
import os
# -*- coding: utf-8 -*-
import sys
reload(sys)
import locale
sys.setdefaultencoding(locale.getpreferredencoding())
os.system("cls")
pattern=re.compile(r"\d{2,}")
proc_list = subprocess.Popen('tasklist', shell=True, stdout=subprocess.PIPE)
nets_list = subprocess.Popen('netstat -ano', shell=True, stdout=subprocess.PIPE)
lines = []
for line in proc_list.stdout:
    if 'PID' in line:
        utext = unicode(line.rstrip(), 'cp1251', 'replace')
        print utext.rstrip()
    if '========' in line:
        print line.rstrip()
    if 'firefox.exe' in line:
        lines.append(line)
        print line.rstrip()
    for id in lines:
        did=pattern.search(id).group(0)
        for netstr in nets_list.stdout:
            if did in netstr:
                print ("\t\t%s"%netstr.rstrip())
exit()

… мне так видно какие порты открвли какие процессы.
Питон тут просто фантастичен вот и сел постигать)))
Вывод
C:\Python27\python.exe C:/001/test11.py
€¬п ®Ўа §  PID €¬п бҐббЁЁ ь ᥠ­б  Џ ¬пвм
========================= ======== ================ =========== ============
firefox.exe 5664 Console 17 520я212 ЉЃ
TCP 127.0.0.1:1701 127.0.0.1:1702 ESTABLISHED 5664
TCP 127.0.0.1:1702 127.0.0.1:1701 ESTABLISHED 5664
TCP 192.168.0.102:4335 109.74.193.139:80 ESTABLISHED 5664
TCP 192.168.0.102:4336 109.74.193.139:80 ESTABLISHED 5664
TCP 192.168.0.102:4337 173.194.113.201:80 ESTABLISHED 5664
TCP 192.168.0.102:4342 50.31.164.173:80 ESTABLISHED 5664

Process finished with exit code 0

… имхо очень удобно

Вот бы еще шапочку поправить ))
))
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