|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:7ce6d9e0e1dc3da5c31fc5f3a5ab7687870a76cd4adaedd6da95bc6451755b12" |
| 4 | + "signature": "sha256:f56b7081a6e5b63610100fcfa0a226c7a0184dfe0d63128614a7a68555653428" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
60 | 60 | "- [Sorting the CSV file](#sorting)\n", |
61 | 61 | "- [Marking min/max values in particular columns](#marking)\n", |
62 | 62 | "- [Writing out the modified table to as a new CSV file](#writing)\n", |
| 63 | + "- [Batch processing CSV files](#batch)\n", |
63 | 64 | "<hr>\n", |
64 | 65 | "<br>\n", |
65 | 66 | "<br>" |
|
646 | 647 | ], |
647 | 648 | "prompt_number": 14 |
648 | 649 | }, |
| 650 | + { |
| 651 | + "cell_type": "markdown", |
| 652 | + "metadata": {}, |
| 653 | + "source": [ |
| 654 | + "<a name='batch'></a>\n", |
| 655 | + "<br>\n", |
| 656 | + "<br>" |
| 657 | + ] |
| 658 | + }, |
| 659 | + { |
| 660 | + "cell_type": "markdown", |
| 661 | + "metadata": {}, |
| 662 | + "source": [ |
| 663 | + "## Batch processing CSV files" |
| 664 | + ] |
| 665 | + }, |
| 666 | + { |
| 667 | + "cell_type": "markdown", |
| 668 | + "metadata": {}, |
| 669 | + "source": [ |
| 670 | + "[[back to top](#sections)]" |
| 671 | + ] |
| 672 | + }, |
| 673 | + { |
| 674 | + "cell_type": "markdown", |
| 675 | + "metadata": {}, |
| 676 | + "source": [ |
| 677 | + "Usually, CSV files never come alone, but we have to process a whole bunch of similar formatted CSV files from some output device. \n", |
| 678 | + "For example, if we want to process all CSV files in a particular input directory and want to save the processed files in a separate output directory, we can use a simple list comprehension to collect tuples of input-output file names." |
| 679 | + ] |
| 680 | + }, |
649 | 681 | { |
650 | 682 | "cell_type": "code", |
651 | 683 | "collapsed": false, |
652 | | - "input": [], |
| 684 | + "input": [ |
| 685 | + "import os\n", |
| 686 | + "\n", |
| 687 | + "in_dir = '../Data'\n", |
| 688 | + "out_dir = '../Data/processed'\n", |
| 689 | + "csvs = [\n", |
| 690 | + " (os.path.join(in_dir, csv), \n", |
| 691 | + " os.path.join(out_dir, csv))\n", |
| 692 | + " for csv in os.listdir(in_dir) \n", |
| 693 | + " if csv.endswith('.csv')\n", |
| 694 | + " ]\n", |
| 695 | + "\n", |
| 696 | + "for i in csvs:\n", |
| 697 | + " print(i)" |
| 698 | + ], |
| 699 | + "language": "python", |
| 700 | + "metadata": {}, |
| 701 | + "outputs": [ |
| 702 | + { |
| 703 | + "output_type": "stream", |
| 704 | + "stream": "stdout", |
| 705 | + "text": [ |
| 706 | + "('../Data/test.csv', '../Data/processed/test.csv')\n", |
| 707 | + "('../Data/test_marked.csv', '../Data/processed/test_marked.csv')\n" |
| 708 | + ] |
| 709 | + } |
| 710 | + ], |
| 711 | + "prompt_number": 12 |
| 712 | + }, |
| 713 | + { |
| 714 | + "cell_type": "markdown", |
| 715 | + "metadata": {}, |
| 716 | + "source": [ |
| 717 | + "<br>\n", |
| 718 | + "Next, we can summarize the processes we want to apply to the CSV files in a simple function and loop over our file names:" |
| 719 | + ] |
| 720 | + }, |
| 721 | + { |
| 722 | + "cell_type": "code", |
| 723 | + "collapsed": false, |
| 724 | + "input": [ |
| 725 | + "def process_csv(csv_in, csv_out):\n", |
| 726 | + " \"\"\" \n", |
| 727 | + " Takes an input- and output-filename of an CSV file\n", |
| 728 | + " and marks minimum values for every column.\n", |
| 729 | + " \n", |
| 730 | + " \"\"\"\n", |
| 731 | + " csv_cont = csv_to_list(csv_in)\n", |
| 732 | + " csv_marked = copy.deepcopy(csv_cont)\n", |
| 733 | + " convert_cells_to_floats(csv_marked)\n", |
| 734 | + " mark_all_col(csv_marked, mark_max=False, marker='*')\n", |
| 735 | + " write_csv(csv_out, csv_marked)" |
| 736 | + ], |
| 737 | + "language": "python", |
| 738 | + "metadata": {}, |
| 739 | + "outputs": [], |
| 740 | + "prompt_number": 18 |
| 741 | + }, |
| 742 | + { |
| 743 | + "cell_type": "code", |
| 744 | + "collapsed": false, |
| 745 | + "input": [ |
| 746 | + "for inout in csvs:\n", |
| 747 | + " process_csv(inout[0], inout[1])" |
| 748 | + ], |
653 | 749 | "language": "python", |
654 | 750 | "metadata": {}, |
655 | 751 | "outputs": [] |
|
0 commit comments