{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# StringIO Objects and the io Module\n", "\n", "Back in **Lecture 24 - Files** we opened files that exist outside of python, and streamed their contents into an in-memory file object. You can also create in-memory file-like objects within your program that Python treats the same way. Text data is stored in a StringIO object, while binary data would be stored in a BytesIO object. This object can then be used as input or output to most functions that would expect a standard file object.\n", "\n", "Let's investigate StringIO objects. The best way to show this is by example:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import io" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Arbitrary String\n", "message = 'This is just a normal string.'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Use StringIO method to set as file object\n", "f = io.StringIO(message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have an object *f* that we will be able to treat just like a file. For example:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is just a normal string.'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also write to it:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "40" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.write(' Second line written to file like object')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Reset cursor just like you would a file\n", "f.seek(0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is just a normal string. Second line written to file like object'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read again\n", "f.read()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Close the object when contents are no longer needed\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! Now you've seen how we can use StringIO to turn normal strings into in-memory file objects in our code. This kind of action has various use cases, especially in web scraping cases where you want to read some string you scraped as a file.\n", "\n", "For more info on StringIO check out the documentation: https://docs.python.org/3/library/io.html" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }