【问题标题】:Get last n lines of file -- decoding error获取文件的最后 n 行 -- 解码错误
【发布时间】:2019-05-21 14:22:32
【问题描述】:

我有一个utf-16 文件,我正在尝试读取其中的最后几行。这是我现在拥有的:

def get_last_n_lines(self, n, file=None):
    '''
    Sorted from the bottom to the top.
    '''
    file = file or self.file
    s = subprocess.check_output(['tail', '-%s' % str(n), file]).decode('utf-8')
    return

但是,我收到以下错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 4607: invalid start byte

即使我这样做.decode('utf-16') 它也会给我一个错误。从 tail 命令获取文件最后 100 行的正确方法是什么?

【问题讨论】:

    标签: python python-3.x unicode tail utf


    【解决方案1】:

    有效的方法是在 decode 方法中添加更通用的 unicode 编码:

    > s = subprocess.check_output(['tail', '-%s' % str(n), file]).decode('unicode_escape')
    

    【讨论】:

      【解决方案2】:

      您可以添加 encoding="437" 或 encoding="850"。

      【讨论】:

        猜你喜欢
        • 2010-09-13
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多