Пример #1
0
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    try:
        fields = int(args["fields"])
    except ValueError:
        print >>sys.stderr, "%s: Need a number of fields/columns." % progname
        sys.exit(1)

    padding = args["padding"]

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Pad the line to have at least as many fields as indicated.

                if len(details) < fields:
                    details += (fields - len(details)) * [padding]

                writer.append(details)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (progname, exc)

    finally:
        reader.close()
Пример #2
0
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >> sys.stderr, "Syntax:"
        print >> sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >> sys.stderr, __doc__
            print >> sys.stderr, "Syntax:"
            print >> sys.stderr, syntax_description
            sys.exit(1)

    try:
        fields = int(args["fields"])
    except ValueError:
        print >> sys.stderr, "%s: Need a number of fields/columns." % progname
        sys.exit(1)

    padding = args["padding"]

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Pad the line to have at least as many fields as indicated.

                if len(details) < fields:
                    details += (fields - len(details)) * [padding]

                writer.append(details)

        except IOError, exc:
            print >> sys.stderr, "%s: %s" % (progname, exc)

    finally:
        reader.close()
Пример #3
0
def main():

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >> sys.stderr, "Syntax:"
        print >> sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >> sys.stderr, __doc__
            print >> sys.stderr, "Syntax:"
            print >> sys.stderr, syntax_description
            sys.exit(1)

    fields = map(index_for_int, args["fields"].split(","))

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Filter the fields.

                details = [
                    details[field] for field in fields if field is not None
                ]

                # Cut the columns according to the field details.

                writer.append(details)

        except IOError, exc:
            print >> sys.stderr, "%s: %s" % (get_progname(), exc)

    finally:
        reader.close()
Пример #4
0
def main():

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    fields = map(index_for_int, args["fields"].split(","))

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Filter the fields.

                details = [details[field] for field in fields if field is not None]

                # Cut the columns according to the field details.

                writer.append(details)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (get_progname(), exc)

    finally:
        reader.close()
Пример #5
0
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    start_field = index_for_int(args.get("start-field", 1))
    end_field = int_or_none(args.get("end-field", None)) # treat 1-index as 0-index plus one

    delimiter = args.get("delimiter", "\t")
    delimiter_within_fields = args.get("delimiter-within-fields", "\t")

    sequence_start = int_or_none(args.get("sequence-start"))

    if args.has_key("filename"):
        filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)

    reader = RawImportFileReader(filename_or_stream, delimiter=delimiter)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Process the data.

                preceding, fields, following = partition(details, start_field, end_field)

                if delimiter_within_fields != delimiter:
                    split_fields = []
                    for field in fields:
                        split_fields += field.split(delimiter_within_fields)
                    fields = split_fields

                for i, field in enumerate(fields):
                    if sequence_start is not None:
                        values = [i + sequence_start, field]
                    else:
                        values = [field]
                    writer.append(preceding + values + following)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (progname, exc)

    finally:
        writer.close()
        reader.close()
Пример #6
0
    # Get the field separator.

    if args.has_key("separator"):
        separator = args["separator"]
    else:
        separator = "\t"

    try:
        pos = 0
        lineno = 0
        current_value = None
        current_value_start = None
        last_value = None

        writer = RawImportFile(rewrite(sys.stdout))

        while 1:
            line = f_in.readline()
            if not line:
                break

            value = line.split(separator)[field]

            # For new values, remember where they first appeared.

            if value != current_value:
                current_value = value
                current_value_start = pos

            # Emit the start of the current value region.
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    start_field = index_for_int(args.get("start-field", 1))
    end_field = int_or_none(args.get("end-field", None)) # treat 1-index as 0-index plus one

    delimiter = args.get("delimiter", "\t")
    delimiter_within_fields = args.get("delimiter-within-fields", "\t")

    sequence_start = int_or_none(args.get("sequence-start"))

    if args.has_key("filename"):
        filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)

    reader = RawImportFileReader(filename_or_stream, delimiter=delimiter)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Process the data.

                preceding, fields, following = partition(details, start_field, end_field)

                if delimiter_within_fields != delimiter:
                    split_fields = []
                    for field in fields:
                        split_fields += field.split(delimiter_within_fields)
                    fields = split_fields

                for i, field in enumerate(fields):
                    if sequence_start is not None:
                        values = [i + sequence_start, field]
                    else:
                        values = [field]
                    writer.append(preceding + values + following)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (progname, exc)

    finally:
        writer.close()
        reader.close()
Пример #8
0
        field = 0

    # Get the field separator.

    if args.has_key("separator"):
        separator = args["separator"]
    else:
        separator = "\t"

    # Get the search term.

    term = args["term"]

    try:
        index = get_index(f_index)
        writer = rewrite(sys.stdout)

        # Search using a tuple in order to compare correctly with the index.

        i = bisect.bisect_left(index, (term, None))

        if i < len(index):
            term, offset = index[i]
            f_data.seek(offset)

            while 1:
                line = f_data.readline()
                if not line:
                    break

                found = line.split(separator)[field]