diff --git a/by_department.csv b/by_department.csv new file mode 100644 index 0000000..606b6c0 --- /dev/null +++ b/by_department.csv @@ -0,0 +1,4 @@ +name,username,department +Sol Mansi,solm,IT infrastructure +Lio Nelson,lion,User Experience Research +Charlie Grey,greyc,Development diff --git a/hosts.csv b/hosts.csv new file mode 100644 index 0000000..477e71e --- /dev/null +++ b/hosts.csv @@ -0,0 +1,2 @@ +workstation.local,192.168.25.46 +webserver.cloud,10.2.5.6 diff --git a/module2.ipynb b/module2.ipynb index 6fbe458..5a61c58 100644 --- a/module2.ipynb +++ b/module2.ipynb @@ -113,7 +113,9 @@ { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "# Reading And Writing CSV" + ] }, { "cell_type": "markdown", @@ -136,11 +138,98 @@ " print(\"Name: {}, Phone: {}, Role: {}\".format(name, phone, role))\n", "f.close()" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reading CSV" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import csv\n", + "f = open(\"csv_file.txt\")\n", + "csv_f = csv.reader(f)\n", + "for row in csv_f:\n", + " name, phone, role = row\n", + " print(\"Name: {}, Phone: {}, Role: {}\".format(name, phone, role))\n", + "f.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Generating CSV" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import csv\n", + "\n", + "hosts = [[\"workstation.local\", \"192.168.25.46\"],[\"webserver.cloud\", \"10.2.5.6\"]]\n", + "with open('hosts.csv', 'w') as hosts_csv:\n", + " writer = csv.writer(hosts_csv)\n", + " writer.writerows(hosts)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reading and writing CSV files with dictionaries" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "users = [ {\"name\": \"Sol Mansi\", \"username\": \"solm\", \"department\": \"IT infrastructure\"}, \n", + " {\"name\": \"Lio Nelson\", \"username\": \"lion\", \"department\": \"User Experience Research\"}, \n", + " {\"name\": \"Charlie Grey\", \"username\": \"greyc\", \"department\": \"Development\"}]\n", + "keys = [\"name\", \"username\", \"department\"]\n", + "with open('by_department.csv', 'w') as by_department:\n", + " writer = csv.DictWriter(by_department, fieldnames=keys)\n", + " writer.writeheader()\n", + " writer.writerows(users)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" } }, "nbformat": 4,