コード例 #1
0
ファイル: wc.py プロジェクト: nettee/shtl
def wc(args):
    file_content = stil.fopen(args.files, strict=True)
    fmt_string = fmt_str(args)

    if file_content is None:   # from <stdin>
        result = count_file(sys.stdin)
        print(fmt_string.format(result))
        return
    else:
        results = []

        for fobj in file_content:
            fname = fobj.name
            if fname == '<stdin>':
                fname = '-'

            result = count_file(fobj)
            results.append(result)
            print(fmt_string.format(result), fname)
        
        if len(file_content) > 1:
            total_result = merge_result(results)
            print(fmt_string.format(total_result), 'total')

    stil.fclose(file_content)
コード例 #2
0
ファイル: ijoin.py プロジェクト: nettee/code_history
import stil

def ijoin(file_content):
    collect = dict()
    for fobj in file_content:
        for line in fobj:
            words = line.rstrip('\n').split()
            foreword = words[0]
            otherwords = '\t'.join(words[1:])
            if foreword in collect:
                collect[foreword].append(otherwords)
            else:
                collect[foreword] = [otherwords]
    
    for foreword in collect:
        print(foreword, end = '\t')
        print('\t'.join(collect[foreword]))

def parse():
    parser = argparse.ArgumentParser()
    parser.add_argument('files', nargs='*')
    return parser.parse_args()

if __name__ == '__main__':
    args = parse()
    file_content = stil.fopen(args.files)

    ijoin(file_content)

    stil.fclose(file_content)
コード例 #3
0
ファイル: cut.py プロジェクト: nettee/shtl
    return sfields

def cut(fobj, args):
    try:
        for line in fobj:
            line = line.rstrip('\n')
            fields = line.split(args.delimiter)
            sfields = extract(fields, args.fields)
            print('\t'.join(sfields))
    except IndexError as err:
        print("Error:", str(err))
    

def parse():
    parser = argparse.ArgumentParser(
            description='remove sections from each line of files')
    parser.add_argument('files', nargs='*')
    parser.add_argument('-d', '--delimiter', default='\t',
            help='use DELIM instead of TAB for field delimiter')
    parser.add_argument('-f', '--fields',
            help='select only these fields')
    return parser.parse_args()

if __name__ == '__main__':
    args = parse()
    file_content = stil.fopen(args.files)
    for fobj in file_content:
        cut(fobj, args)
    stil.fclose(file_content)