diff --git a/getting_started/02_text_io.ipynb b/getting_started/02_text_io.ipynb index c3b6aa2378cb6294aaded9b955e0ab5fc9fe5335..b6181731bbe95856547422cd6ed7b54f1ce36b70 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 9628a213a271c882faa431cf63e2977912d475bb..0c00651c3afd011823348b5cc5f7eadcb5344c04 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 0000000000000000000000000000000000000000..f5fce0dafc3a5ac00aaecf1544e197a2f863eb1f --- /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