|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## CSV Vs EXCEL :\n", |
| 8 | + " - CSV file can’t perform operations on data while Excel can perform operations on the data.\n", |
| 9 | + " - CSV files are faster and also consumes less memory whereas Excel consumes more memory while importing data.\n", |
| 10 | + " - CSV files can be opened with any text editor in windows while Excel files can't be opened with text editors." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "## FileObject from open function" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "## The mode for reader Object is read-binary ('rb') and for writer Object is write-binary ('wb')" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 2, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "import csv\n", |
| 34 | + "with open('movies.csv', 'w', newline='') as file:\n", |
| 35 | + " writer = csv.writer(file)\n", |
| 36 | + "## writerow function takes a list argument and writes it to a CSV file\n", |
| 37 | + " writer.writerow([\"SrNum\", \"Movie\", \"Genre\"])\n", |
| 38 | + " writer.writerow([1, \"Venom\", \"Sci-Fi\"])\n", |
| 39 | + " writer.writerow([2, \"A Quiet Place\", \"Horror\"])" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 3, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [ |
| 47 | + { |
| 48 | + "name": "stdout", |
| 49 | + "output_type": "stream", |
| 50 | + "text": [ |
| 51 | + "['SrNum', 'Movie', 'Genre']\n", |
| 52 | + "['1', 'Venom', 'Sci-Fi']\n", |
| 53 | + "['2', 'A Quiet Place', 'Horror']\n" |
| 54 | + ] |
| 55 | + } |
| 56 | + ], |
| 57 | + "source": [ |
| 58 | + "## to check contents of csv\n", |
| 59 | + "with open('movies.csv', 'r') as file:\n", |
| 60 | + " reader = csv.reader(file)\n", |
| 61 | + " for row in reader:\n", |
| 62 | + " print(row)" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "## The delimiter argument used to separate data bydefault comma(,) in it. A lineterminator is a string used to terminate lines produced by writer objects. The default value is \\r\\n. You can change its value by passing any string as a lineterminator parameter." |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 9, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "name": "stdout", |
| 79 | + "output_type": "stream", |
| 80 | + "text": [ |
| 81 | + "{\"name\": \"John\", \"age\": 31, \"Salary\": 25000}\n" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "data": { |
| 86 | + "text/plain": [ |
| 87 | + "str" |
| 88 | + ] |
| 89 | + }, |
| 90 | + "execution_count": 9, |
| 91 | + "metadata": {}, |
| 92 | + "output_type": "execute_result" |
| 93 | + } |
| 94 | + ], |
| 95 | + "source": [ |
| 96 | + "import json \n", |
| 97 | + " \n", |
| 98 | + "# {key:value mapping} ## Its type is dictonary \n", |
| 99 | + "a ={\"name\":\"John\", \n", |
| 100 | + " \"age\":31, \n", |
| 101 | + " \"Salary\":25000} \n", |
| 102 | + "# conversion to JSON done by dumps() function \n", |
| 103 | + "b = json.dumps(a) \n", |
| 104 | + "print(b) \n", |
| 105 | + "type(b)" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 5, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "a ={\"name\":\"John\", \n", |
| 115 | + " \"age\":31, \n", |
| 116 | + " \"Salary\":25000} " |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": 6, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "data": { |
| 126 | + "text/plain": [ |
| 127 | + "dict" |
| 128 | + ] |
| 129 | + }, |
| 130 | + "execution_count": 6, |
| 131 | + "metadata": {}, |
| 132 | + "output_type": "execute_result" |
| 133 | + } |
| 134 | + ], |
| 135 | + "source": [ |
| 136 | + "type(a)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 10, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "json_var =\"\"\" \n", |
| 146 | + "{ \n", |
| 147 | + " \"Employee\": { \n", |
| 148 | + " \"name\": \"Akash Borgalli\", \n", |
| 149 | + " \"Languages_spoken\": [ \n", |
| 150 | + " { \n", |
| 151 | + " \"names\": [\"Hindi\", \"English\", \"Kanada\", \"Marathi\"] \n", |
| 152 | + " } \n", |
| 153 | + " ] \n", |
| 154 | + " } \n", |
| 155 | + "} \n", |
| 156 | + "\"\"\"\n", |
| 157 | + "## json loads() is a function that takes a string of JSON data and returns a dictionary data structure\n", |
| 158 | + "var = json.loads(json_var) " |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": 16, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [ |
| 166 | + { |
| 167 | + "name": "stdout", |
| 168 | + "output_type": "stream", |
| 169 | + "text": [ |
| 170 | + "<class 'str'>\n", |
| 171 | + "{'Employee': {'name': 'Akash Borgalli', 'Languages_spoken': [{'names': ['Hindi', 'English', 'Kanada', 'Marathi']}]}}\n" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "data": { |
| 176 | + "text/plain": [ |
| 177 | + "dict" |
| 178 | + ] |
| 179 | + }, |
| 180 | + "execution_count": 16, |
| 181 | + "metadata": {}, |
| 182 | + "output_type": "execute_result" |
| 183 | + } |
| 184 | + ], |
| 185 | + "source": [ |
| 186 | + "print(type(json_var))\n", |
| 187 | + "print(var)\n", |
| 188 | + "type(var)" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "execution_count": 15, |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [ |
| 196 | + { |
| 197 | + "data": { |
| 198 | + "text/plain": [ |
| 199 | + "str" |
| 200 | + ] |
| 201 | + }, |
| 202 | + "execution_count": 15, |
| 203 | + "metadata": {}, |
| 204 | + "output_type": "execute_result" |
| 205 | + } |
| 206 | + ], |
| 207 | + "source": [ |
| 208 | + "type(json_var)" |
| 209 | + ] |
| 210 | + } |
| 211 | + ], |
| 212 | + "metadata": { |
| 213 | + "kernelspec": { |
| 214 | + "display_name": "Python 3", |
| 215 | + "language": "python", |
| 216 | + "name": "python3" |
| 217 | + }, |
| 218 | + "language_info": { |
| 219 | + "codemirror_mode": { |
| 220 | + "name": "ipython", |
| 221 | + "version": 3 |
| 222 | + }, |
| 223 | + "file_extension": ".py", |
| 224 | + "mimetype": "text/x-python", |
| 225 | + "name": "python", |
| 226 | + "nbconvert_exporter": "python", |
| 227 | + "pygments_lexer": "ipython3", |
| 228 | + "version": "3.8.5" |
| 229 | + } |
| 230 | + }, |
| 231 | + "nbformat": 4, |
| 232 | + "nbformat_minor": 4 |
| 233 | +} |
0 commit comments