Simple CSV File Parser


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import os
  2. import csv
  3.  
  4. data = csv.reader(open('source.csv'))
  5. fields = data.next()
  6. output = '<?php %s$countries = array(%s' % (os.linesep, os.linesep)
  7. for row in data:
  8. if row[0]:
  9. output += '%s"%s%s" => array("country" => "%s", "email" => "%s"),' \
  10. % (os.linesep, row[1], row[4], row[0], row[5])
  11. output = output[:-1] + '); ?>'
  12.  
  13. f = open('/path/to/destination.php', 'w')
  14. f.write(output)
  15. f.close()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.