Repository for Petra's work at ampli Jan-Feb 2019

pickletocsv.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435
  1. from argparse import ArgumentParser, FileType
  2. from sys import stdout
  3. import pandas as p
  4. def main():
  5. parser = ArgumentParser(description='Transform a "pickle" to a csv file')
  6. parser.add_argument(dest="input", help = "input pickle path", type = FileType('rb'))
  7. parser.add_argument(dest="output", nargs="?", help = "output csv path", type = FileType('w'))
  8. parser.add_argument("-r", "--row-names", dest = "rownames", help = "include row names in csv; "
  9. "needed to faithfully reproduce some dataframes. "
  10. "Use -v to check information about dataframe", action = "store_true")
  11. parser.add_argument("-v", "--verbose", dest = "verbose", action ="store_true",
  12. help="print information about dataframe and process")
  13. args = parser.parse_args()
  14. if args.output is None:
  15. args.output = stdout
  16. if args.verbose:
  17. print("Reading pickle")
  18. ptc = p.read_pickle(args.input)
  19. if args.verbose:
  20. print(ptc.info())
  21. print("Saving as csv")
  22. ptc.to_csv(args.output, index = args.rownames)
  23. args.input.close()
  24. args.output.close()
  25. if __name__ == "__main__":
  26. main()