Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法grep python程序的输出,可能是utf-16_Python_Grep_Utf 16 - Fatal编程技术网

无法grep python程序的输出,可能是utf-16

无法grep python程序的输出,可能是utf-16,python,grep,utf-16,Python,Grep,Utf 16,我编写了一个基本的python程序来解析android的resources.arsc。它打印出文件中找到的所有字符串。字符串在每个字符之间有一个零值字节。这对我来说意味着字符串存储在utf-16中。我不知道这是否正确,但android字符串是可本地化的,所以我认为是这样。我正在使用string.decode('hex')以人类可读的格式打印字符串。下面是一个示例,其中包含组成字符串的字节列表: >>> print ''.join(['00', '72', '00', '65',

我编写了一个基本的python程序来解析android的resources.arsc。它打印出文件中找到的所有字符串。字符串在每个字符之间有一个零值字节。这对我来说意味着字符串存储在utf-16中。我不知道这是否正确,但android字符串是可本地化的,所以我认为是这样。我正在使用string.decode('hex')以人类可读的格式打印字符串。下面是一个示例,其中包含组成字符串的字节列表:

>>> print ''.join(['00', '72', '00', '65', '00', '73', '00', '2f', '00', '64', '00', '72',    '00', '61', '00', '77', '00', '61', '00', '62', '00', '6c', '00', '65', '00', '2f', '00', '61', '00', '62', '00', '6f', '00', '75', '00', '74', '00', '2e', '00', '70', '00', '6e', '00', '67', '00', '00', '00']).decode('hex')
res/drawable/about.png
问题是,当我将这个程序传输到grep时,我无法grep读取的任何字符串。如何将其打印到shell中,以便grep能够匹配其输出?谢谢

(编辑)我确实打印了字符串,但在我的示例中,我认为最好同时显示“打印”版本和返回的版本。很抱歉给你带来了困惑。在本例中,无法灰色化的是“/res/drawable/about.png”

(编辑2)一个简单的演示:

11:33 AM ~/learning_python $ python -c "print ''.join(['00', '72', '00', '65', '00', '73', '00', '2f', '00', '64', '00', '72',    '00', '61', '00', '77', '00', '61', '00', '62', '00', '6c', '00', '65', '00', '2f', '00', '61', '00', '62', '00', '6f', '00', '75', '00', '74', '00', '2e', '00', '70', '00', '6e', '00', '67', '00', '00', '00']).decode('hex')"
res/drawable/about.png
11:33 AM ~/learning_python $ python -c "print ''.join(['00', '72', '00', '65', '00', '73', '00', '2f', '00', '64', '00', '72',    '00', '61', '00', '77', '00', '61', '00', '62', '00', '6c', '00', '65', '00', '2f', '00', '61', '00', '62', '00', '6f', '00', '75', '00', '74', '00', '2e', '00', '70', '00', '6e', '00', '67', '00', '00', '00']).decode('hex')" | grep about
11:33 AM ~/learning_python $ 
(编辑3)另一个演示,我认为这证明了数据在utf-16-be中:

11:33 AM ~/learning_python $ python -c "print ''.join(['00', '72', '00', '65', '00', '73', '00', '2f', '00', '64', '00', '72',    '00', '61', '00', '77', '00', '61', '00', '62', '00', '6c', '00', '65', '00', '2f', '00', '61', '00', '62', '00', '6f', '00', '75', '00', '74', '00', '2e', '00', '70', '00', '6e', '00', '67', '00', '00', '00']).decode('hex')" > testfile
11:35 AM ~/learning_python $ iconv -f utf16be -t utf8 testfile
res/drawable/about.png
11:35 AM ~/learning_python $ iconv -f utf16be -t utf8 testfile | grep about
Binary file (standard input) matches
11:35 AM ~/learning_python $ iconv -f utf16be -t utf8 testfile | grep -a about
res/drawable/about.png
解码字符:

'\x00r\x00e\x00s'.decode('utf-16-be') # produces u'res'
然后可以打印出解码的字符串:

$ python -c "print ''.join(['00', '72', '00', '65', '00', '73', '00', '2f', '00', '64', '00', '72',    '00', '61', '00', '77', '00', '61', '00', '62', '00', '6c', '00', '65', '00', '2f', '00', '61', '00', '62', '00', '6f', '00', '75', '00', '74', '00', '2e', '00', '70', '00', '6e', '00', '67', '00', '00', '00', '00']).decode('hex').decode('utf-16-be').rstrip('\0')" | grep about
res/drawable/about.png
使用而不是可以支持UTF-16文件的
grep

ripgrep支持以UTF-8以外的文本编码搜索文件,如UTF-16、拉丁语-1、GBK、EUC-JP、Shift_JIS等。(提供了自动检测UTF-16的一些支持。必须使用
-E
/
--encoding标志指定其他文本编码。

示例语法:

rg sometext file

你“打印”了解码后的字符串吗?是的,最终的字符串就是这样产生的。为了清晰起见,我编辑了我的问题。将所有内容放在一个数组中,您将注意到prob python-c“print[''.join”([00',72',00',65',00',73',00',2f',00',64',00',72',00',61',00',77',00',61',62',00',6c',00',65',00',2f',00',61',00',62',00',6f',00',75',00',00',74',00',2e',00',70',00',67',00',00',00',00',00',00',00',00',00',00',00'我现在可以grep程序的输出,但我必须使用-a开关。我可以接受:)