Guest User

Untitled

a guest
Mar 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Loads a CSV file, assuming it has header names, and prints only the specified columns.
  3. # ... like selecting two columns of a 7-column table.
  4. #
  5. # Arguments:
  6. # - field names, comma-separated
  7. # - input filename
  8. #
  9. # Output:
  10. # - the header row for the specified columns, and those fields for all input records.
  11. #
  12. # Usage:
  13. # python csvextract.py "fieldName1,fieldName2" ./source.csv > output.csv
  14. #
  15. #
  16. #
  17.  
  18. import sys
  19. import csv
  20. import fileinput
  21.  
  22.  
  23. def parse_field_expression(expr):
  24. return expr.split(',')
  25.  
  26. def perform_extract(field_expr, filename):
  27. _input = fileinput.input(filename)
  28. reader = csv.DictReader(_input)
  29. expression = parse_field_expression(field_expr)
  30. for row in reader:
  31. yield dict((e, row[e]) for e in expression)
  32.  
  33.  
  34. def main(args):
  35. writer = None
  36. fieldnames = None
  37. for record in perform_extract(args[0], args[1]):
  38. if writer is None:
  39. fieldnames = record.keys()
  40. writer = csv.DictWriter(sys.stdout,
  41. fieldnames=fieldnames)
  42. try:
  43. writer.writerow(record)
  44. except ValueError, e:
  45. print >>sys.stderr, repr(record)
  46. raise e
  47.  
  48. if __name__ == '__main__':
  49. main(sys.argv[1:])
Add Comment
Please, Sign In to add comment