Skip to content

Commit

Permalink
Keep the order of header fields
Browse files Browse the repository at this point in the history
  • Loading branch information
raimon49 committed May 11, 2019
1 parent 456f22e commit a2c89c5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def get_string(self, **kwargs):
return json.dumps(lines, indent=2, sort_keys=True)


class CSVPrettyTable(JsonPrettyTable):
class CSVPrettyTable(PrettyTable):
"""PrettyTable-like class exporting to CSV"""

def get_string(self, **kwargs):
Expand All @@ -275,10 +275,12 @@ def esc_quotes(val):
formatted_rows = self._format_rows(rows, options)

lines = []
for index, row in enumerate(formatted_rows):
values = row.values() if index else row.keys()
formatted_header = ','.join(['"%s"' % (esc_quotes(val), )
for val in self._field_names])
lines.append(formatted_header)
for row in formatted_rows:
formatted_row = ','.join(['"%s"' % (esc_quotes(val), )
for val in values])
for val in row])
lines.append(formatted_row)

return '\n'.join(lines)
Expand Down
2 changes: 1 addition & 1 deletion test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def test_format_csv(self):
output_string = create_output_string(args)

obtained_header = output_string.split('\n', 1)[0]
expected_header = '"Version","Name","License","Author"'
expected_header = '"Name","Version","License","Author"'
self.assertEqual(obtained_header, expected_header)

def test_from_compatibility(self):
Expand Down

0 comments on commit a2c89c5

Please sign in to comment.