From eace3436b2f3e5abf37bc11834160e06b34276a2 Mon Sep 17 00:00:00 2001 From: Michiel Cottaar <MichielCottaar@gmail.com> Date: Sat, 10 Feb 2018 12:34:31 +0000 Subject: [PATCH] Added exercises --- getting_started/02_text_io.ipynb | 24 +++++++++++++++++++++++- getting_started/02_text_io.md | 14 +++++++++++++- getting_started/02_text_io/input.txt | 20 ++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 getting_started/02_text_io/input.txt diff --git a/getting_started/02_text_io.ipynb b/getting_started/02_text_io.ipynb index c3b6aa2..b618173 100644 --- a/getting_started/02_text_io.ipynb +++ b/getting_started/02_text_io.ipynb @@ -599,7 +599,29 @@ "<a class=\"anchor\" id=\"exercises\"></a>\n", "## Exercises\n", "### Joining/splitting strings\n", - "go from 2 column file to 2 rows\n", + "The file 02_text_io/input.txt contains integers in a 2-column format (separated by spaces). Read in this file and write it back out in 2-rows separated by comma's." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "input_filename = '02_text_io/input.txt'\n", + "out_filename = '02_text_io/output.txt'\n", + "\n", + "with open(input_filename, 'r') as input_file:\n", + " ...\n", + "\n", + "with open(output_filename, 'w') as output_file:\n", + " ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "### String formatting and regular expressions\n", "Given a template for MRI files:\n", "s<subject_id>/<modality>_<res>mm.nii.gz\n", diff --git a/getting_started/02_text_io.md b/getting_started/02_text_io.md index 9628a21..0c00651 100644 --- a/getting_started/02_text_io.md +++ b/getting_started/02_text_io.md @@ -275,7 +275,19 @@ A full discussion of regular expression goes far beyond this practical. If you a <a class="anchor" id="exercises"></a> ## Exercises ### Joining/splitting strings -go from 2 column file to 2 rows +The file 02_text_io/input.txt contains integers in a 2-column format (separated by spaces). Read in this file and write it back out in 2-rows separated by comma's. + +``` +input_filename = '02_text_io/input.txt' +out_filename = '02_text_io/output.txt' + +with open(input_filename, 'r') as input_file: + ... + +with open(output_filename, 'w') as output_file: + ... +``` + ### String formatting and regular expressions Given a template for MRI files: s<subject_id>/<modality>_<res>mm.nii.gz diff --git a/getting_started/02_text_io/input.txt b/getting_started/02_text_io/input.txt new file mode 100644 index 0000000..f5fce0d --- /dev/null +++ b/getting_started/02_text_io/input.txt @@ -0,0 +1,20 @@ +1 3 +14 3 +4 7 +12 5 +15 5 +14 4 +15 19 +7 17 +7 15 +16 3 +15 14 +17 9 +7 8 +4 5 +15 4 +11 5 +17 10 +3 19 +14 4 +18 3 -- GitLab