{"cells":[{"cell_type":"markdown","id":"12b02b53","metadata":{},"source":["<h2>Files I/O</h2>\n","<hr>\n","<p>We've been working in our separate code files, sometimes importing code. Now we'll focus on getting data both to and from external files</p>\n","We'll start with a few simple examples where we write names into a document and the read them line by line"]},{"cell_type":"code","execution_count":2,"id":"c913485b","metadata":{},"outputs":[],"source":["#Writing in an external file, simples form\n","name = input(\"Name: \")\n","#The 'w' meens we're open for writing (overwriting old stuff)\n","file = open(\"names.txt\", \"w\")\n","file.write(name)\n","file.close()"]},{"cell_type":"markdown","id":"11675f5c","metadata":{},"source":["When we do this we either open the named file overwriting the content with a new line, or we create a new document and add the new line.\n","If we instead use \"a\", we can append instead of overwrite"]},{"cell_type":"code","execution_count":5,"id":"bfef8e9a","metadata":{},"outputs":[],"source":["#Writing in an external file, simplest form\n","name = input(\"Name: \")\n","#The 'a' meens we're open for appending\n","file = open(\"names.txt\", \"a\")\n","file.write(f\"{name}\\n\")\n","file.close()"]},{"cell_type":"markdown","id":"08c86e00","metadata":{},"source":["If we run this twice we get 2 separate lines with our names:\n","<br>\n","Kurre\n","<br>\n","Pelle\n","<br>\n","This works ok, but we have to remeber to close tyhe file... there's an easier way.\n","<h4>Using <code>with</code>\n"]},{"cell_type":"code","execution_count":null,"id":"ee34385d","metadata":{},"outputs":[],"source":["#Writing using with, opens and closes automatically\n","name = input(\"Name: \")\n","with open(\"names.txt\", \"a\") as file:\n","    file.write(f\"{name}\\n\")"]},{"cell_type":"code","execution_count":6,"id":"d1b406e9","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["hello, Kurre\n","hello, Pelle\n"]}],"source":["#Reading using with and creating list, stripping whitespace we don't need\n","names = []\n","with open(\"names.txt\") as file:\n","    for line in file:\n","        names.append(line.rstrip())\n","\n","for name in sorted(names):\n","    print(f\"hello, {name}\")"]},{"cell_type":"markdown","id":"f036685d","metadata":{},"source":["<p>This is great, but sometimes we need to have a bit more structure. We could use JSON, but just for fun, lets go with another really common format.</p>\n","<h4>Using the format <code>CSV</code>"]},{"cell_type":"code","execution_count":3,"id":"49d8507f","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Anders is in Sweden\n","Ben is in Netherlands\n","Einstein is in Germany\n","Leto is in Belgium\n","Name is in Country\n","Samuel is in Sweden\n","Virul is in Sri Lanka\n"]}],"source":["#Read from our csv-file, creating list, sorting and displaying\n","students = []\n","\n","with open(\"students.csv\") as file:\n","    for line in file:\n","        name, land = line.rstrip().split(\",\")\n","        students.append(f\"{name} is in {land}\")\n","\n","for student in sorted(students):\n","    print(student)"]},{"cell_type":"code","execution_count":4,"id":"b55567e4","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Ben is in Netherlands\n","Samuel is in Sweden\n","Anders is in Sweden\n","Leto is in Belgium\n","Virul is in Sri Lanka\n","Einstein is in \"Germany West\"\n"]}],"source":["#If we instead want to save the students as small dictionaries\n","students = []\n","with open(\"students.csv\") as file:\n","    for line in file:\n","        name, land = line.rstrip().split(\",\")\n","        student = {\"name\": name, \"land\": land}\n","        students.append(student)\n","\n","for student in students:\n","    print(f\"{student['name']} is in {student['land']}\")"]},{"cell_type":"markdown","id":"04cb42a6","metadata":{},"source":["But these aren't sorted... lets fix this by implementing a search-fix"]},{"cell_type":"code","execution_count":5,"id":"ba8cf41e","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Anders is in Sweden\n","Ben is in Netherlands\n","Einstein is in \"Germany West\"\n","Leto is in Belgium\n","Samuel is in Sweden\n","Virul is in Sri Lanka\n"]}],"source":["students = []\n","\n","with open(\"students.csv\") as file:\n","    for line in file:\n","        name, land = line.rstrip().split(\",\")\n","        students.append({\"name\": name, \"land\": land})\n","\n","\n","def get_name(student):\n","    return student[\"name\"]\n","\n","\n","for student in sorted(students, key=get_name):\n","    print(f\"{student['name']} is in {student['land']}\")"]},{"cell_type":"markdown","id":"07cd065a","metadata":{},"source":["This can be even further improved. We don't need to have a separate function, we can use the generic term <code>lambda</code>"]},{"cell_type":"code","execution_count":4,"id":"f3b243ab","metadata":{},"outputs":[{"ename":"ValueError","evalue":"too many values to unpack (expected 2)","output_type":"error","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)","\u001b[1;32m/Users/randlerm2/Documents/UU och Campus/Undervisning UU/Kurser 23-24/23 Coding in Python /Lectures/L7/files-i-o.ipynb Cell 14\u001b[0m line \u001b[0;36m5\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/randlerm2/Documents/UU%20och%20Campus/Undervisning%20UU/Kurser%2023-24/23%20Coding%20in%20Python%20/Lectures/L7/files-i-o.ipynb#X16sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mstudents2.csv\u001b[39m\u001b[39m\"\u001b[39m) \u001b[39mas\u001b[39;00m file:\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/randlerm2/Documents/UU%20och%20Campus/Undervisning%20UU/Kurser%2023-24/23%20Coding%20in%20Python%20/Lectures/L7/files-i-o.ipynb#X16sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m     \u001b[39mfor\u001b[39;00m line \u001b[39min\u001b[39;00m file:\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/randlerm2/Documents/UU%20och%20Campus/Undervisning%20UU/Kurser%2023-24/23%20Coding%20in%20Python%20/Lectures/L7/files-i-o.ipynb#X16sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m         name, land \u001b[39m=\u001b[39m line\u001b[39m.\u001b[39mrstrip()\u001b[39m.\u001b[39msplit(\u001b[39m\"\u001b[39m\u001b[39m,\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/randlerm2/Documents/UU%20och%20Campus/Undervisning%20UU/Kurser%2023-24/23%20Coding%20in%20Python%20/Lectures/L7/files-i-o.ipynb#X16sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m         students\u001b[39m.\u001b[39mappend({\u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: name, \u001b[39m\"\u001b[39m\u001b[39mland\u001b[39m\u001b[39m\"\u001b[39m: land})\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/randlerm2/Documents/UU%20och%20Campus/Undervisning%20UU/Kurser%2023-24/23%20Coding%20in%20Python%20/Lectures/L7/files-i-o.ipynb#X16sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m \u001b[39mfor\u001b[39;00m student \u001b[39min\u001b[39;00m \u001b[39msorted\u001b[39m(students, key\u001b[39m=\u001b[39m\u001b[39mlambda\u001b[39;00m student: student[\u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m]):\n","\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 2)"]}],"source":["students = []\n","\n","with open(\"students2.csv\") as file:\n","    for line in file:\n","        name, land = line.rstrip().split(\",\")\n","        students.append({\"name\": name, \"land\": land})\n","\n","for student in sorted(students, key=lambda student: student[\"name\"]):\n","    print(f\"{student['name']} is in {student['land']}\")"]},{"cell_type":"markdown","id":"fbbdff31","metadata":{},"source":["Lets add a line to our csv that has 2 commas..., this will of course generate an error.\n","We'll fix this by once again leaning on a library\n","<h4>Using the CSV-module in Python</h4>"]},{"cell_type":"code","execution_count":7,"id":"c2d3d911","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Anders is from Sweden\n","Ben is from Netherlands\n","Einstein is from Germany,West\n","Leto is from Belgium\n","Samuel is from Sweden\n","Virul is from Sri Lanka\n","name is from land\n"]}],"source":["import csv\n","\n","students = []\n","\n","with open(\"students2.csv\") as file:\n","    reader = csv.reader(file)\n","    for row in reader:\n","        students.append({\"name\": row[0], \"land\": row[1]})\n","\n","for student in sorted(students, key=lambda student: student[\"name\"]):\n","    print(f\"{student['name']} is from {student['land']}\")"]},{"cell_type":"markdown","id":"e079844c","metadata":{},"source":["We could take this one step further using <code>DictReader</code>"]},{"cell_type":"code","execution_count":6,"id":"bc31b713","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Anders is in Sweden\n","Ben is in Netherlands\n","Einstein is in Germany,West\n","Leto is in Belgium\n","Samuel is in Sweden\n","Virul is in Sri Lanka\n"]}],"source":["import csv\n","\n","students = []\n","\n","with open(\"students2.csv\") as file:\n","    reader = csv.DictReader(file)\n","    for row in reader:\n","        students.append({\"name\": row[\"name\"], \"land\": row[\"land\"]})\n","\n","for student in sorted(students, key=lambda student: student[\"name\"]):\n","    print(f\"{student['name']} is in {student['land']}\")"]},{"cell_type":"markdown","id":"62d3a2cf","metadata":{},"source":["Ok, but what about writing using the CSV-module?"]},{"cell_type":"code","execution_count":20,"id":"8b3ac045","metadata":{},"outputs":[],"source":["import csv\n","students = []\n","while True:\n","    try:\n","        name = input(\"What's your name? \")\n","        land = input(\"What's your home country? \")\n","        students.append({\"name\":name,\"land\":land})\n","    except EOFError:\n","        break\n","\n","with open(\"students4.csv\", \"a\") as file:\n","    writer = csv.DictWriter(file, fieldnames=[\"name\", \"land\"])\n","    for student in students:\n","        writer.writerow({\"name\": student[\"name\"], \"land\":student[\"land\"]})"]}],"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.11.4"}},"nbformat":4,"nbformat_minor":5}
