-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdel_col.py
executable file
·62 lines (56 loc) · 1.98 KB
/
del_col.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
61
#!/usr/bin/env python3
#
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
# #
# del_col.py #
# #
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
#
# Author: Pat Prodanovic, Ph.D., P.Eng.
#
# Date: Jan 22, 2017
#
# Purpose: Takes in a comma separated *.csv file and deletes the
# specified column from the file. It works only for pputils *.csv
# files, that have number values as columns. It uses numpy to read
# and write the file.
#
# Uses: Python 2 or 3, Numpy
#
# Example:
#
# python del_col.py -i input.csv -c 0 -o output.csv
#
# where:
#
# -n original input file (comma delimited)
# -c column to crop (column index starts at zero)
# -o output file with the column cropped
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Global Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os,sys
import numpy as np
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# MAIN
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# I/O
if len(sys.argv) != 7 :
print('Wrong number of Arguments, stopping now...')
print('Usage:')
print('python del_col.py -i input.csv -c 0 -o output.csv')
sys.exit()
input_file = sys.argv[2]
col = int(sys.argv[4])
output_file = sys.argv[6]
# to create the output file
# fout = open(output_file,'w')
# read input file
input_data = np.loadtxt(input_file, delimiter=',',skiprows=0,unpack=True)
# delete the specified column from the numpy array input_data
# note the indices to be deleted have to be specified as a list
output_data = np.delete(input_data, [col], axis=0)
# writes the final output (delimited by a comma, three digits after the decimal
np.savetxt(output_file, np.transpose(output_data), fmt='%.3f', delimiter=',')