{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Функция возвращает список строк, прочитанный из файла на удаленном сервере:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing url_open.py\n" ] } ], "source": [ "%%writefile url_open.py\n", "import urllib.request\n", "\n", "def url_open(url:str) -> list:\n", " \"\"\"\n", " Функция возвращает список строк, прочитанный из файла на удаленном сервере.\n", " \n", " \"\"\"\n", " lst = list() \n", " \n", " with urllib.request.urlopen(url) as webpage:\n", " # где webpage - файловый объект, кот. содержит информацию об открываемом файле.\n", " for line in webpage: \n", " # преобразуем тип bytes в str с исп. кодировки utf-8 \n", " lst.append(line.decode('utf-8').strip())\n", " return lst " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Теперь можем импортировать модуль:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from url_open import url_open" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['But soft what light through yonder window breaks', 'It is the east and Juliet is the sun', 'Arise fair sun and kill the envious moon', 'Who is already sick and pale with grief']\n" ] } ], "source": [ "print(url_open(\"http://dfedorov.spb.ru/python3/src/romeo.txt\"))" ] }, { "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }