-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort_csv.py
executable file
·60 lines (49 loc) · 1.57 KB
/
sort_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Generate CSV from config file
Usage:
sort_csv.py --input <path-to-csv> --output <path-to-csv> [--sort <column>] [--verbose]
sort_csv.py (-h | --help)
sort_csv.py --version
Options:
-h, --help Show this screen.
--version Show version.
-i, --input <path-to-csv> Path to the input CSV file.
-o, --output <path-to-csv> Path to the output CSV file.
-s, --sort <column> Name of the column to sort by [default: col_date].
-v, --verbose Verbose output.
"""
import sys
import os
import logging
import pandas as pd
import csv
from docopt import docopt
arguments = docopt(__doc__, version='Sort CSV file 1.0')
# Parameter
input_path = arguments['--input']
output_path = arguments['--output']
column = arguments['--sort']
verbose = arguments['--verbose']
# Logging
log = logging.getLogger(__name__)
if verbose:
log.setLevel(logging.DEBUG)
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
logging.captureWarnings(True)
try:
# read CSV in dataframr
df = pd.read_csv(input_path)
# sort the dataframe
columns = [c.strip() for c in column.split(',')]
log.debug("Sort CSV using the column '{columns}'")
df_sorted = df.sort_values(columns)
# export sorted dataframe as CSV
df_sorted.to_csv(output_path, index=False, quoting=csv.QUOTE_NONNUMERIC)
except Exception:
log.exception(f"Error in sort_csv.py with input {input_path}, output {output_path}")
sys.exit(1)