Skip to content

Instantly share code, notes, and snippets.

@JeffSackmann
Created January 13, 2011 20:01
Show Gist options
  • Save JeffSackmann/778484 to your computer and use it in GitHub Desktop.
Save JeffSackmann/778484 to your computer and use it in GitHub Desktop.
turn a 2-d python matrix (list of lists) into a .csv file
## turn a 2-d python matrix (list of lists) into a .csv file
## see also gist: 778481 to reverse the process
def writeMatrixCSV(mx, fname):
## mx is a 2-d python matrix
## fname is the desired output filename
f = open(fname, 'w')
for r in mx:
tx = ''
i = 0
while i < (len(r)-1):
tx += str(r[i])
tx += ','
i += 1
try: tx += str(r[(len(r)-1)])
except: pass
if tx == '': pass
else: tx += '\n'
f.write(tx)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment