Browse Source

Edit pickle-csv converter script

Petra Lamborn 5 years ago
parent
commit
ed38891649
2 changed files with 14 additions and 8 deletions
  1. 3
    3
      README.md
  2. 11
    5
      py/pickletocsv.py

+ 3
- 3
README.md View File

@@ -142,15 +142,15 @@ Aggregates data from `../data/test1k.pkl` by cluster information in `../data/tes
142 142
 
143 143
 Helper function to transform a pickle into a csv file, for easier importing into e.g. Excel.
144 144
 
145
-* `-i PATH`: The path for the python "pickle" file which contains the dataset.
146
-* `-o PATH`: The path for the csv file to store the dataset in; if omitted prints to `stdout`.
145
+* `input`: The path for the python "pickle" file which contains the dataset.
146
+* `output`: The path for the csv file to store the dataset in; if omitted, or `-`, prints to `stdout`.
147 147
 * `-r`: Include row names/index labels in csv. This may be essential for proper exporting of some datasets
148 148
 * `-v`: Output extra information, including dimensions of dataset.
149 149
 
150 150
 Example:
151 151
 
152 152
 ```bash
153
-python pickletocsv.py -i ../data/test1kagg.pkl | less
153
+python pickletocsv.py ../data/test1kagg.pkl | less
154 154
 ```
155 155
 
156 156
 Reads file at `../data/test1kagg.pkl` and views it in the UNIX pager `less`.

+ 11
- 5
py/pickletocsv.py View File

@@ -1,13 +1,16 @@
1
-from argparse import ArgumentParser
1
+from argparse import ArgumentParser, FileType
2 2
 from sys import stdout
3 3
 import pandas as p
4 4
 
5 5
 def main():
6 6
     parser = ArgumentParser(description='Transform a "pickle" to a csv file')
7
-    parser.add_argument("-i", "--input",  dest="input",      help = "input pickle path",  metavar="PATH", required = True)
8
-    parser.add_argument("-o", "--output", dest="output",     help = "output csv path", metavar="PATH")
9
-    parser.add_argument("-r", "--row-names", dest = "rownames", help = "include row names in csv; if ommitted prints to stdout", action = "store_true")
10
-    parser.add_argument("-v", "--verbose", dest = "verbose", action ="store_true")
7
+    parser.add_argument(dest="input",      help = "input pickle path", type = FileType('rb'))
8
+    parser.add_argument(dest="output",  nargs="?",   help = "output csv path", type = FileType('w'))
9
+    parser.add_argument("-r", "--row-names", dest = "rownames", help = "include row names in csv; "
10
+                        "needed to faithfully reproduce some dataframes. "
11
+                        "Use -v to check information about dataframe", action = "store_true")
12
+    parser.add_argument("-v", "--verbose", dest = "verbose", action ="store_true", 
13
+                        help="print information about dataframe and process")
11 14
     args = parser.parse_args()
12 15
 
13 16
     if args.output is None:
@@ -24,6 +27,9 @@ def main():
24 27
 
25 28
     ptc.to_csv(args.output, index = args.rownames)
26 29
 
30
+    args.input.close()
31
+    args.output.close()
32
+
27 33
 
28 34
 if __name__ == "__main__":
29 35
     main()