简体   繁体   English

从指定字节偏移量的文件中获取行

[英]Get line from file at specified byte offset

I have a file with a bunch of lines. 我有一堆文件。 I have a list of the bytes offsets corresponding with the start of each line. 我有一个与每行开头对应的字节偏移列表。 I want each line that corresponds with the byte offset. 我希望每行与字节偏移量相对应。 Is there a way to do this in unix, perl or python? 有没有办法在unix,perl或python中执行此操作? I have to do this at a much larger scale than described. 我必须以比描述更大的规模来做这件事。

File: 文件:

abcd
bcde
cdef

Byte Offsets: 字节偏移:

0
10

Desired Output: 期望的输出:

abcd
cdef
with open(filename, 'r') as f:    
    for offset in offsets:
        f.seek(offset)
        print(f.readline())

References: 参考文献:

Quickie perl: Quickie perl:

my @offsets = ( 0, 10 );

open (my $data, '<', 'file.txt') || die "Can't open input: $!\n";

foreach my $offset (@offsets) 
{
    seek( $data, $offset, 0 );
    my $line = <$data>;
    print $line;
}

close $data;

When I ended up with (thanks to unutbu) 当我结束时(感谢unutbu)

#!/usr/bin/python
f = open(file_name, 'r')
offsets = [0,10]
for offset in offsets:
    f.seek(offset)
    print f.readline().strip()

seek() to the required byte position, then read. seek()到所需的字节位置,然后读取。 This should be easy from Python and Perl, and doable from shell script (I'm thinking dd ). 这应该很容易从Python和Perl,并可以从shell脚本(我在想dd )。

This should do it. 这应该做到这一点。

def get_lines_by_offset(filename, *offsets):
    with open(filename, "r") as fp:
        results = []
        for offset in offsets:
            fp.seek(offset)
            results.append(fp.readline().strip())
    return results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM