See More

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "os.path 에는 파일이나 디렉토리가 실제로 존재하는지 확인하는 exists 함수가 있습니다. 만약 디렉토리가 존재하지 않는다면 os.makedirs 를 이용하여 디렉토리를 만들 수 있습니다." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "if not os.path.exists('data'):\n", " os.makedirs('data')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File open\n", "\n", "파일 내 텍스트를 읽기 위해서는 우선 파일과의 통로를 열어야 합니다. `with` 는 파일을 연상태를 의미하며 `as f` 는 열려있는 통로의 이름을 `f` 로 정의한다는 의미입니다. `import seaborn as sns` 에서의 `as` 와 같은 기능을 합니다. `readlines()` 는 텍스트의 한 줄을 하나의 str 로 읽어들입니다.\n", "\n", "Python에서 파일을 읽거나 쓸 때 (File I/O)는 open을 이용하면 됩니다. 이 때 반드시 인코딩을 utf-8로 지정하는 것을 추천합니다. \n", "\n", "인코딩이란 쉽게 말해서 'a'라는 글자를 어떤 숫자로 저장할 것이냐라는 언어표 입니다. 컴퓨터는 정보를 2진수로 이뤄진 숫자로 저장합니다. 'a'라는 글자는 어떤 인코딩에서는 0일 수도, 다른 어떤 인코딩에서는 100 일 수도 있습니다. 그리고 각 인코딩이 표현할 수 있는 언어의 폭도 다릅니다. 처음 컴퓨터가 만들어질 때 나왔던 인코딩은 한글, 중국어 등의 세계 각 국의 언어는 고려하지 않았습니다. 영어/숫자/특수기호 등으로만 이뤄진 인코딩을 이용하였지만, 컴퓨터를 사용하는 지역이 넓어짐에 따라 각 언어를 반영하도록 인코딩은 계속 발전하였습니다. \n", "\n", "현재에는 utf-8이 표준 인코딩으로 이용되고 있으며, 여기에는 한국어를 포함한 많은 언어들이 등록되어 있습니다. \n", "\n", "그런데 컴퓨터 입장에서 001010 처럼 써진 숫자들만 보고 이 숫자가 어떤 글자를 의미하는지 추측하려면 때로는 틀릴 수가 있습니다. 반대로 사용자가 001010을 어떤 인코딩으로 읽으라고 지정할 수 있습니다. 또한 파일에 데이터를 쓸 때에도 'a'라는 글자를 어떤 인코딩 방식을 통해서 이진수로 표현할지를 지정할 수 있습니다. \n", "\n", "파이썬은 해당 OS의 기본 인코딩을 사용합니다. 윈도우의 기본 인코딩은 cp949이며, ubuntu, MacOS에서 이 포멧이 잘 열리지 않을 수 있기 때문에, 표준 인코딩인 utf-8을 명시하여 인코딩 혼동이 없도록 함을 추천합니다." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "7\n", "# 데이터 처리를 위한 기본 코드\n" ] } ], "source": [ "with open('README.md', encoding='utf-8') as f:\n", " doc = f.readlines()\n", "\n", "print(type(doc)) # \"\"\n", "print(len(doc)) # \"7\"\n", "print(doc[0].strip()) # \"# 데이터 처리를 위한 기본 코드\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List 내 각 str 의 마지막 글자는 줄바꿈 기호인 '\\n' 입니다." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['# 데이터 처리를 위한 기본 코드\\n', '\\n', '- Python basic (Data type, functions, class, file IO)\\n', '- Numpy (ndarray, slice, matrix handling)\\n', '- Seaborn tutorial\\n', '- Bokeh tutorial\\n', '- Pandas tutorial (Series, DataFrame, merge, io, slice, select, groupby, handling NaN)\\n']\n" ] } ], "source": [ "print(doc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List 를 깔끔하게 출력하기 위하여 pprint 를 이용할 수도 있습니다." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['# 데이터 처리를 위한 기본 코드\\n',\n", " '\\n',\n", " '- Python basic (Data type, functions, class, file IO)\\n',\n", " '- Numpy (ndarray, slice, matrix handling)\\n',\n", " '- Seaborn tutorial\\n',\n", " '- Bokeh tutorial\\n',\n", " '- Pandas tutorial (Series, DataFrame, merge, io, slice, select, groupby, '\n", " 'handling NaN)\\n']\n" ] } ], "source": [ "from pprint import pprint\n", "\n", "pprint(doc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "혹은 `str.join` 을 이용하면 doc 을 하나의 str 로 만들 수 있습니다." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# 데이터 처리를 위한 기본 코드\n", "\n", "- Python basic (Data type, functions, class, file IO)\n", "- Numpy (ndarray, slice, matrix handling)\n", "- Seaborn tutorial\n", "- Bokeh tutorial\n", "- Pandas tutorial (Series, DataFrame, merge, io, slice, select, groupby, handling NaN)\n", "\n" ] } ], "source": [ "print(''.join(doc))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List of str 의 파일을 `data/README_copy.md` 에 저장해 봅니다." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "with open('data/README_copy.md', 'w', encoding='utf-8') as f:\n", " for line in doc:\n", " f.write(f'{line}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON\n", "\n", "JSON 형식의 데이터를 텍스트 파일로 저장할 수 있습니다." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'key': 'value', 'key2': ['list', 'form'], 'key3': {'dict': 'form'}}\n" ] } ], "source": [ "import json\n", "\n", "d2 = {\n", " 'key': 'value',\n", " 'key2': ['list', 'form'],\n", " 'key3': {'dict': 'form'}\n", "}\n", "\n", "print(d2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "JSON 형식으로 데이터를 저장할 때에는 파일을 연 뒤, `json.dump(data, filepath)` 를 입력합니다." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "with open('data/json_flat.json', 'w', encoding='utf-8') as f:\n", " json.dump(d2, f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IPython notebook 에서는 몇 가지 linux 명령어를 지원합니다. 파일의 내용을 출력하는 `cat` 을 이용하여 파일의 내용을 확인합니다. 데이터가 한 줄로 기록되어 있습니다. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"key\": \"value\", \"key2\": [\"list\", \"form\"], \"key3\": {\"dict\": \"form\"}}" ] } ], "source": [ "%cat data/json_flat.json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "JSON 파일을 데이터로 읽을 때에는 `json.load` 를 이용합니다." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'key': 'value', 'key2': ['list', 'form'], 'key3': {'dict': 'form'}}\n" ] } ], "source": [ "with open('data/json_flat.json', encoding='utf-8') as f:\n", " loaded_d2 = json.load(f)\n", "\n", "print(loaded_d2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "데이터 내에 한글로 이뤄진 텍스트를 추가한 뒤, 다시 JSON 으로 저장합니다." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "d2['key3'] = '한글 글자'\n", "with open('data/json_flat_ascii.json', 'w', encoding='utf-8') as f:\n", " json.dump(d2, f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "한글 부분이 제대로 기록되지 않습니다." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"key\": \"value\", \"key2\": [\"list\", \"form\"], \"key3\": \"\\ud55c\\uae00 \\uae00\\uc790\"}" ] } ], "source": [ "%cat data/json_flat_ascii.json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "이번에는 한글 (영어, 숫자 등 기본 글자를 제외한 모든 글자) 이 한글로 기록되도록 `ensure_ascii=False` 로 설정하고, 각 항목별로 가독성을 높이기 위하여 `indent=2` 로 설정합니다. 계층구조에 따라 2칸 들여쓰기를 한다는 의미입니다." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "with open('data/json_indent.json', 'w', encoding='utf-8') as f:\n", " json.dump(d2, f, indent=2, ensure_ascii=False)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\r\n", " \"key\": \"value\",\r\n", " \"key2\": [\r\n", " \"list\",\r\n", " \"form\"\r\n", " ],\r\n", " \"key3\": \"한글 글자\"\r\n", "}" ] } ], "source": [ "%cat data/json_indent.json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "JSON 데이터를 str 로 변환하기 위해서는 `json.dumps` 를 이용합니다." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"key\": \"value\", \"key2\": [\"list\", \"form\"], \"key3\": \"한글 글자\"}'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.dumps(d2, ensure_ascii=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "반대로 str 형식의 데이터를 JSON 으로 파싱하기 위해서는 `json.loads` 를 이용합니다." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'key': 'value', 'key2': ['list', 'form'], 'key3': '한글 글자'}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.loads(\n", " json.dumps(d2, ensure_ascii=False)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pickle\n", "\n", "pickle을 저장/읽는 방식은 open() 에서 'wb', 'rb'를 하는 것입니다. wb는 write binary 의 의미이고, rb는 read binary 의 의미입니다. Binary 파일이기 때문에 텍스트로 읽을 수는 없습니다." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'key': 'value', 'key2': ['list', 'form'], 'key3': '한글 글자'}\n" ] } ], "source": [ "import pickle\n", "\n", "with open('data/json_data.pkl', 'wb') as f: # write as byte\n", " pickle.dump(d2, f)\n", "\n", "with open('data/json_data.pkl', 'rb') as f: # read as byte\n", " loaded_d2 = pickle.load(f)\n", "\n", "print(loaded_d2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }