在Python中的subprocess.Popen中指定stdout中的文本类型

问题描述 投票:0回答:1

首先对不起,如果标题不尽可能具有描述性,很难解释。

我已经对批处理进行了子进程调用,并使用print()将所有内容重定向到python控制台,批处理执行打印信息消息,并且我需要打印自定义消息,仅当批处理消息是确定的消息时。但从某种程度上说,python检测到的字符串不同。

我不知道它是否是编码问题(我在readline编码iso-8859-1以避免utf-8的编码错误)。

这是我的代码。

from tkinter import *
    from tkinter import ttk
    from subprocess import Popen, PIPE
    import os


    gui = Tk()

    text = Text(gui)
    lb = ttk.Label(text="My bat")

    cmd = 'C:\\Users\\User\\Desktop\\BAT1.bat'

    def runbat():
        proc = Popen(cmd,shell=False,stdout=PIPE)
        while True:
            line = proc.stdout.readline().decode('iso-8859-1').rstrip('\n')

            if line != '':
                myline = 'INFO Starting: Initialize communication activity (InitializeCommunication).'
                if line == myline:
                    print("That's the line!")
                print(line)
            else:
                break

    bt = ttk.Button(text="Run bat", command=runbat).grid(row=4, column=5)

    text.grid(row=6, column= 5)
    lb.grid(row=3, column=5)
    prog.grid(row=7,column=5)
    mainloop()

这是输出:

C:\Python34\python.exe "C:/Users/User/Documents/Desarrollo/Boos Production Manager/preferences/multioneproof.py"

C:\Users\User\Documents\Desarrollo\Boos Production Manager\preferences>cd /D C: 

C:\Users\User\Documents\Desarrollo\Boos Production Manager\preferences>cd C:\Program Files\Philips MultiOne Workflow 

C:\Program Files\Philips MultiOne Workflow>MultiOneWorkflow.exe /f "C:/Users/User/Desktop/A.xml" /w "Z:\Spain Factory\multione configuration\verify.txt" /p S /lu true /v info /c Halt 
WARN Parameter IncludeUniqueIdOfDeviceInLabelData is provided without the GenerateAndExportLabelData parameter.
INFO Philips MultiOne Workflow version 3.11.91.28
INFO OS: Microsoft Windows 10 Home. Computer name: PC-DAVID. Application path: C:\Program Files\Philips MultiOne Workflow\MultiOneWorkflow.exe. Running as administrator: no. Format: Espa¤ol (Espa¤a) [es-ES], date format: dd/MM/yyyy, right to left: no, decimal separator: [,].
INFO Key: N/A. Profile: Debug. TwelveNc: N/A. 
INFO Privileges: 0-10V / 1-10V: All. 0-10V / 1-10V (LED Driver): All. ActiLume: All. ActiLume wired: All. ActiLume wireless: All. Active Cooling: All. Adjustable Light Output: All. Adjustable Light Output Minimum: All. Adjustable Output Current: All. Adjustable Output Current Multi-Channel: All. Adjustable Startup Time: All. AmpDim: All. Coded Light: All. Coded Light Pwm: All. Coded Light Randomize: All. Coded Mains Scene Settings: All. Coded Mains Standalone Receiver: All. ComBox: All. Constant Light Output: All. Constant Light Output LITE: All. Constant Light Output Multi-Channel: All. Correlated Color Temperature Dual Channel: All. Correlated color temperature: All. Corridor Mode: All. DALI 102 variables: All. DALI Power Supply: All. DC Emergency: All. Dali 202 variables: All. Daylight override / Daylight switching: All. Device Info: All. Diagnostics: All. Diagnostics Emergency: All. Diagnostics Motor Controller: All. Dimming Interface: All. Driver Addressing: All. Driver Temperature Limit: All. Dwell Time: All. Dynadimmer: All. Emergency: All. End Of Life indication: All. Energy Meter: All. FCC Test Mode Settings: All. Factory link: All. Field Task Tuning: All. Field Task Tuning/Occupancy Sensing/Daylight Harvesting: All. Lamp Burn-in: All. Lamp selection: All. Late Stage Configuration: All. Light Source Age: All. LineSwitch: All. Load Fault Indicator Thresholds: All. Logical Signal Input: All. Lumen Level: All. LumiStep: All. Luminaire (Fixture) Information: All. Luminaire Production Test: All. Min dim level: All. Module Temperature Protection: All. Motor Control: All. NTC on LedSet: All. OEM Write Protection: All. Occupancy / Daylight: All. Occupancy sharing / Group light behavior: All. PowerBox: All. Push Button Unit LCU2070: All. Push Button Unit LCU2071: All. Quick Lamp Start: All. Relay Switched Output: All. SR Power Supply: All. Set Lamp uptime: All. Step Dimming: All. Touch and Dim: All. 
INFO On warnings: halt
INFO Using Write&Verify.
INFO Multiple device configuring: Disabled
INFO Commission all: Disabled
INFO Check device model: Enabled
INFO DALI factory new: Disabled
INFO Starting: Prepare system activity (PrepareSystem).
INFO Success: Prepare system activity (PrepareSystem).
INFO Starting: Select feature file activity (OpenFile).
INFO Opening features file
INFO Provided file: c:/users/user/desktop/a.xml
INFO Success: Select feature file activity (OpenFile).
INFO Starting: Initialize communication activity (InitializeCommunication).
INFO Success: Initialize communication activity (InitializeCommunication).
INFO Starting: Identify device activity (IdentifyDevice).
INFO Devices identified: 0
ERROR No connected devices were found
ERROR Failure: Identify device activity (IdentifyDevice).
INFO Starting: Stop activity (Stop).
INFO Success: Stop activity (Stop).
INFO End

C:\Program Files\Philips MultiOne Workflow>echo 500  1>"Z:\Spain Factory\multione configuration\log.txt" 

Process finished with exit code 0

所以我认为它必须在正确的行上打印我的自定义句子,但由于某种原因它不会这样做。

python batch-file tkinter subprocess
1个回答
3
投票
if line != '' :
    myline = 'INFO Starting: Initialize communication activity (InitializeCommunication).'
    if str(myline) in str(line) :
        print("That's the line!")
    print(line)
else :
    break

在这个你的'line'变量值有额外的尾随空格,这不是'myline'变量,这就是为什么你的等于if条件失败,如果你在条件中使用它将解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.