【问题标题】:Python - Android logcat output of stdout to textview then save to .txt file errorPython - 标准输出的 Android logcat 输出到 textview 然后保存到 .txt 文件错误
【发布时间】:2016-01-28 15:53:13
【问题描述】:

我有以下 Python 代码,它通过标准输出使用 Android logcat 将日志从手机中提取到 wxPython 文本字段中。 我登录到我的应用程序,用户名 jack 和密码 Jack@123$ logcat 输出停止并出现错误,它似乎不像 @ 或 $ 符号:

    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 76: character maps to <undefined>

或者当我尝试将文本控件的 GetValue() 保存到一个可以写入的 .txt 文件中时:

UnicodeEncodeError: 'ascii' codec can't encode characters in position  204447-204449: ordinal not in range(128)

logcat函数代码如下

def logcat(self):
   params = [toolsDir + "\\adb.exe", "logcat"]
   p = Popen(params, stdout=subprocess.PIPE, bufsize=1)
   for line in p.stdout:
       #line = line.decode('Latin-1')
       self.progressBox.AppendText(line)

def saveLog(self,e):
    f = open(outDir + '\\' + pkgName + '\\' + pkgName + '_logcat.txt', 'w')
    f.write(self.progressBox.GetValue())
    f.close()

使用标准输出对这些符号进行解码的正确方法是什么? 谢谢

【问题讨论】:

  • 我通过使用以下内容获得了一些进一步的信息,for line in p.stdout: linedec=line.decode('utf-8') self.progressBox.AppendText(linedec) 但是在写入文件时我仍然卡住了。 UnicodeEncodeError: 'charmap' codec can't encode character u'\uae08' in position 107257: character m aps to &lt;undefined&gt; 尽管在写入文件之前进行了解码。

标签: android python wxpython logcat


【解决方案1】:

编辑: 答案如下,我必须将 stdout 中的 decode('utf-8') 解码为文本框,然后写入文件时进行 encode('utf-8')。

    for line in p.stdout:
        self.progressBox.AppendText(line.decode('utf-8'))


    f.write(self.progressBox.GetValue().encode('utf-8'))

【讨论】:

    猜你喜欢
    • 2019-02-27
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多