按以往python2的习惯编码输出报错

 #-*- coding:utf-8 -*-
'''
Created on 2018年7月21日 @author: lenovo
'''
import os
import sys
import subprocess
from uiautomator import device as d
cmd = r'adb install F:\听力.apk'
info = subprocess.check_output(cmd).split("\r\n")
print (info)

输出如下,报错

 Traceback (most recent call last):
File "C:\Users\lenovo\eclipse-workspace\WOO\src\debug\debug1.py", line 12, in <module>
info = subprocess.check_output(cmd).split("\r\n")
TypeError: a bytes-like object is required, not 'str'

查询python3文档有下面描述:

By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.
也就是说,返回的其实是一个编码后的比特值,实际的编码格式取决于调用的命令,因此python3将解码过程交给应用层,也就是我们使用的人来做啦。

证实过程是不是这样,我们把这个subprocess.check_output()的类型打印出来如下,确实为bytes类型,需要人为再转换一次为string

 #-*- coding:utf-8 -*-
'''
Created on 2018年7月21日 @author: lenovo
'''
import os
import sys
import subprocess
from uiautomator import device as d
cmd = r'adb install F:\听力.apk'
info = subprocess.check_output(cmd)
print (type(info))

输出如下:

<class 'bytes'>

正确的如下:

 #-*- coding:utf-8 -*-
'''
Created on 2018年7月21日 @author: lenovo
'''
import os
import sys
import subprocess
from uiautomator import device as d
cmd = r'adb install F:\听力.apk'
info = subprocess.check_output(cmd)
info1 = info.decode()
print (info1.split('\r\n'))

输出如下:

['Success', '']

[Python3]subprocess.check_output() 在python3的输出为bytes而非string,在实际使用过程中得增加一个解码过程decode(),不然会有问题的更多相关文章

  1. subprocess.run()用法python3.7

    def run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs): "&q ...

  2. 【学习笔记】第七章 python3核心技术与实践--输入与输出

    [第六章]思考题答案,仅供参考: # coding:utf-8import time#方法一start_time = time.perf_counter()s = ''for n in range(0 ...

  3. 【Android自动化】Subprocess.check_output()简单用法

    # -*- coding:utf-8 -*- import os import sys import subprocess from uiautomator import device as d cm ...

  4. 转 python3中SQLLIT编码与解码之Unicode与bytes

    #########sample########## sqlite3.OperationalError: Could not decode to UTF-8 column 'logtype' with ...

  5. Python3使用过程中需要注意的点

    命名规则 变量 变量名只能是数字.字母或下划线的任意组合 变量名的第一个字符不能是数字 不能使用关键字作为变量名 变量的定义要具有可描述性 变量名不宜过长.不宜使用中文.拼音 常量(常用在配置文件中) ...

  6. python3中的编码与解码(超好理解)

    编码和解码是针对数据而言的,数据能干什么呢?无非就是用来显示,储存和传输的: 储存和传输数据当然是希望数据越小越好,所以发明了utf-8这种数据编码显示:它智能将英文用一个字节表示,欧洲的字符用两个字 ...

  7. linux下python2升级python3,python2和python3并存

    wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz 解压:tar -xzvf Python-3.6.4.tgz cd Pytho ...

  8. centos7安装Python3的过程中会和Python2.7版本冲突导致yum版本比对应,致使yum不能使用的问题。

    centos7安装Python3的过程中会和Python2.7版本冲突导致yum版本比对应,致使yum不能使用的问题. 原因:yum调用Python,启动程/usr/bin/yum就是一个python ...

  9. centos6.5下 python3.6安装、python3.6虚拟环境

    https://www.cnblogs.com/paladinzxl/p/6919049.html # python3.6的安装 wget https://www.python.org/ftp/pyt ...

随机推荐

  1. 表格(Table)隔行变色

    在ASP.NET的Repeater控件,实现隔行变色,是极简单的事情.因为它有ListItemType.Item和ListItemType.AlternatingItem模版.如果在普通的表格(Tab ...

  2. RadioButtonList根据值触发OnSelectedIndexChanged事件

    Insus.NET有使用Iframe来处理另外一个站点的enter form,由于需要自动循环填入数据,免去人手操作.但是原来的Enter from有RadioButtonList控件以及OnSele ...

  3. MVC的注意事项及开发技巧

    演示产品源码下载地址:http://www.jinhusns.com 

  4. thinkphp ajax删除 隐藏与显示

    知识点: 1.ajax删除: 2.一个同步实现三个异步的效果. html 部分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...

  5. [日常] Go语言圣经-Panic异常,Recover捕获异常习题

    Go语言圣经-Panic异常1.当panic异常发生时,程序会中断运行,并立即执行在该goroutine中被延迟的函数(defer 机制)2.不是所有的panic异常都来自运行时,直接调用内置的pan ...

  6. [日常] Go语言圣经-错误,函数值习题

    Go语言圣经-错误 1.panic异常.panic是来自被调函数的信号,表示发生了某个已知的bug 2.任何进行I/O操作的函数都会面临出现错误的可能 3.错误是软件包API和应用程序用户界面的一个重 ...

  7. 乐字节-Java8新特性-接口默认方法之Stream流(下)

    接上一篇:<Java8新特性之stream>,下面继续接着讲Stream 5.流的中间操作 常见的流的中间操作,归为以下三大类:筛选和切片流操作.元素映射操作.元素排序操作: 操作 描述 ...

  8. 深入理解 Java Object

    Java中的Object对象为所有对象的直接或间接父对象,里面定义的几个方法容易被忽略却非常重要.以下来自Effective Java 对Object中几个关键方法的应用说明. public clas ...

  9. Gold Rush(hnu13249)

    Gold Rush Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KB Total submit users: 15 ...

  10. Crazy Shopping(拓扑排序+完全背包)

    Crazy Shopping(拓扑排序+完全背包) Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C. ...