Skip to content

Instantly share code, notes, and snippets.

@thomaswilley
Last active December 20, 2015 22:29
Show Gist options
  • Save thomaswilley/6205384 to your computer and use it in GitHub Desktop.
Save thomaswilley/6205384 to your computer and use it in GitHub Desktop.
convert a csv file to an array
def csvfile_to_array(csvfilepath, delimiter=',', ignore_header=False, set_required_num_cols=(True, 4)):
results = []
with open(csvfilepath, 'r') as csvfile:
ctareader = csv.reader(csvfile, delimiter=delimiter)
if ignore_header:
next(ctareader, None)
for row in ctareader:
if set_required_num_cols[0]:
# hackishly doubly check for None and for cols in one pass
if len([v for v in row if v]) == set_required_num_cols[1]:
results.append(row)
else:
results.append(row)
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment