コード例 #1
0
ファイル: test_common.py プロジェクト: jeongyoonlee/rosetta
 def test_get_list_from_filerows(self):
     infile = StringIO("1\n2\n#3\n\n5")
     result = common.get_list_from_filerows(infile)
     self.assertEqual(result, ["1", "2", "5"])
コード例 #2
0
 def test_get_list_from_filerows(self):
     infile = StringIO("1\n2\n#3\n\n5")
     result = common.get_list_from_filerows(infile)
     self.assertEqual(result, ['1', '2', '5'])
コード例 #3
0
ファイル: cut.py プロジェクト: Ashleybishop37/CIDM6325
def main():
    epilog = r"""

    Examples
    ---------
    Read a comma delimited csv file, data.csv, keep the 'name' column
    $ cut.py -k name test/commafile.csv

    Read a comma delimited csv file, data.csv, remove 'name', 'age' columns
    $ cut.py -r name,age test/commafile.csv
    """
    parser = argparse.ArgumentParser(
        description=globals()['__doc__'], epilog=epilog,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument(
        'infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin,
        help='Convert this file.  If not specified, read from stdin.')
    parser.add_argument(
        '-o', '--outfile', default=sys.stdout, type=argparse.FileType('w'),
        help='Write to OUT_FILE rather than sys.stdout.')

    specs = parser.add_mutually_exclusive_group(required=True)
    specs.add_argument(
        "-k", "--keep_list",
        help="Only keep columns in this (comma delimited) list.")
    specs.add_argument(
        "--keep_file",
        help="Only keep columns whose name appears in this "
        "(newline delimited) file. (# lines are comments)")
    specs.add_argument(
        "-r", "--remove_list",
        help="Remove columns in this (comma delimited) list.")
    specs.add_argument(
        "--remove_file",
        help="Remove columns whose name appears in this "
        "(newline delimited) file. (# lines are comments)")

    parser.add_argument(
        "-d", "--delimiter",
        help="Use DELIMITER as the column delimiter in infile."
        " For tabs use one of -d t  -d tab -d \\t -d '\\t'"
        "  [default: %(default)s]", default=',')

    args = parser.parse_args()

    ## Handle the options
    # These 4 (keep/remove options) are enforced as mutually exclusive by
    # argparse
    if args.keep_list:
        keep_list = args.keep_list.split(',')
    elif args.keep_file:
        keep_list = common.get_list_from_filerows(args.keep_file)
    else:
        keep_list = None

    if args.remove_list:
        remove_list = args.remove_list.split(',')
    elif args.remove_file:
        remove_list = common.get_list_from_filerows(args.remove_file)
    else:
        remove_list = None

    # Deal with tabs
    if args.delimiter in ['t', '\\t', '\t', 'tab']:
        args.delimiter = '\t'

    ## Call the function that does the real work
    cut_file(
        args.infile, args.outfile, delimiter=args.delimiter,
        keep_list=keep_list, remove_list=remove_list)
コード例 #4
0
ファイル: cut.py プロジェクト: Koltrane/rosetta
def main():
    epilog = """

    Examples
    ---------
    Read a comma delimited csv file, data.csv, keep the 'name' column
    $ python cut.py -k name test/commafile.csv

    Read a comma delimited csv file, data.csv, remove 'name', 'age' columns
    $ python cut.py -r name,age test/commafile.csv

    Keep the "name" column in a a tab delimited dataset
    $ python cut.py -d'\t' -k name  test/tabfile.csv
    Note that -dt  -dtab -d\t -d'\t' -d\\t  also work
    """
    parser = argparse.ArgumentParser(
        description=globals()['__doc__'], epilog=epilog,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument(
        'infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin,
        help='Convert this file.  If not specified, read from stdin.')
    parser.add_argument(
        '-o', '--outfile', default=sys.stdout, type=argparse.FileType('w'),
        help='Write to OUT_FILE rather than sys.stdout.')

    specs = parser.add_mutually_exclusive_group(required=True)
    specs.add_argument(
        "-k", "--keep_list",
        help="Only keep columns in this (comma delimited) list.")
    specs.add_argument(
        "--keep_file",
        help="Only keep columns whose name appears in this "
        "(newline delimited) file. (# lines are comments)")
    specs.add_argument(
        "-r", "--remove_list",
        help="Remove columns in this (comma delimited) list.")
    specs.add_argument(
        "--remove_file",
        help="Remove columns whose name appears in this "
        "(newline delimited) file. (# lines are comments)")

    parser.add_argument(
        "-d", "--delimiter",
        help="Use DELIMITER as the column delimiter in infile."
        "  [default: %(default)s]", default=',')

    args = parser.parse_args()

    ## Handle the options
    # These 4 (keep/remove options) are enforced as mutually exclusive by
    # argparse
    if args.keep_list:
        keep_list = args.keep_list.split(',')
    elif args.keep_file:
        keep_list = common.get_list_from_filerows(args.keep_file)
    else:
        keep_list = None

    if args.remove_list:
        remove_list = args.remove_list.split(',')
    elif args.remove_file:
        remove_list = common.get_list_from_filerows(args.remove_file)
    else:
        remove_list = None

    # Deal with tabs
    if args.delimiter in ['t', '\\t', '\t', 'tab']:
        args.delimiter = '\t'

    ## Call the function that does the real work
    cut_file(
        args.infile, args.outfile, delimiter=args.delimiter,
        keep_list=keep_list, remove_list=remove_list)