Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions prettytable/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,28 @@ def clear(self):
def copy(self):
return copy.deepcopy(self)

def to_csv(self, filename, headers=True):
"""Save PrettyTable results to a CSV file.

Adapted from @AdamSmith https://stackoverflow.com/questions/32128226

Arguments:

filename - filepath for the output CSV
headers - whether to include the header row in the CSV
"""
raw = self.get_string()
data = [tuple(filter(None, map(str.strip, splitline)))
for line in raw.splitlines()
for splitline in [line.split('|')] if len(splitline) > 1]
if self.title is not None:
data = data[1:]
if not headers:
data = data[1:]
with open(filename, 'w') as f:
for d in data:
f.write('{}\n'.format(','.join(d)))

##############################
# MISC PRIVATE METHODS #
##############################
Expand Down