Skip to content

Commit

Permalink
added option --key_line to csv2yml to take care of cases when keys ar…
Browse files Browse the repository at this point in the history
…e not in 1st header line
  • Loading branch information
GuenterQuast committed Feb 19, 2023
1 parent 28fd814 commit fd3f8a5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions PhyPraKit/PhyPraKit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,7 @@ def plot_xy(x, y, ex, ey, title=None,
# finally, plot legend of all shown graphs
plt.legend(loc='best')

def csv2yaml(file, nlhead=1, delim='\t'):
def csv2yaml(file, nlhead=1, delim='\t', keyline=0):
"""read floating point data in general csv format and convert to yaml
skip header lines, replace decimal comma, remove special characters,
Expand Down Expand Up @@ -2217,7 +2217,7 @@ def specialCharFilter(f, delim):
#print(" --> number of data points", Nlin)

# interpret strings in header[0] as keys
keys = hlines[0].split(delim)
keys = hlines[keyline].split(delim)
Nkey = len(keys)
if Nkey != Ncol:
print('Nkey=', Nkey, ' Ncol=', Ncol)
Expand Down
6 changes: 5 additions & 1 deletion PhyPraKit/csv2yml.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
parser.add_argument('-n','--header_lines',
type=int, default=1,
help="numer of header lines, default=1")
parser.add_argument('-k','--key_line',
type=int, default=0,
help="header line containing keys, default=0")

if len(sys.argv)==1: # print help message if no input given
parser.print_help()
Expand All @@ -61,6 +64,7 @@
fnam = args.filename

nlhead = args.header_lines
l_key = args.key_line
delim = args.delimiter
showHeader = args.Header
sav_flg = args.save
Expand All @@ -70,7 +74,7 @@
# ---- end argument parsing -------------------

f = open(fnam, 'r')
hlines, ylines = csv2yaml(f, nlhead, delim)
hlines, ylines = csv2yaml(f, nlhead, delim, l_key)

if not quiet:
print('-->', fnam, 'to yaml', end='')
Expand Down
6 changes: 5 additions & 1 deletion tools/csv2yml.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
parser.add_argument('-n','--header_lines',
type=int, default=1,
help="numer of header lines, default=1")
parser.add_argument('-k','--key_line',
type=int, default=0,
help="header line containing keys, default=0")

if len(sys.argv)==1: # print help message if no input given
parser.print_help()
Expand All @@ -61,6 +64,7 @@
fnam = args.filename

nlhead = args.header_lines
l_key = args.key_line
delim = args.delimiter
showHeader = args.Header
sav_flg = args.save
Expand All @@ -70,7 +74,7 @@
# ---- end argument parsing -------------------

f = open(fnam, 'r')
hlines, ylines = csv2yaml(f, nlhead, delim)
hlines, ylines = csv2yaml(f, nlhead, delim, l_key)

if not quiet:
print('-->', fnam, 'to yaml', end='')
Expand Down

0 comments on commit fd3f8a5

Please sign in to comment.