Created
October 30, 2023 14:19
-
-
Save ksur/ff9a319e9b30bcd74af89bb29c00cb3c to your computer and use it in GitHub Desktop.
faker_zapis_danych_testowych.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyPwi8CEXCDAYx1pt0GiDea6", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ksur/ff9a319e9b30bcd74af89bb29c00cb3c/faker_zapis_danych_testowych.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Faker. Przykłady zapisu danych testowych" | |
], | |
"metadata": { | |
"id": "ukU2rtQi8Hl0" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install faker" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "6CTvcLMPuHqL", | |
"outputId": "cc80dda0-afd8-4fcd-dc2d-c41c7433658d" | |
}, | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Requirement already satisfied: faker in /usr/local/lib/python3.10/dist-packages (19.12.0)\n", | |
"Requirement already satisfied: python-dateutil>=2.4 in /usr/local/lib/python3.10/dist-packages (from faker) (2.8.2)\n", | |
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.4->faker) (1.16.0)\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Zapis danych testowych do pliku csv" | |
], | |
"metadata": { | |
"id": "vjUkm9S78MzM" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from faker import Faker\n", | |
"fake = Faker()" | |
], | |
"metadata": { | |
"id": "5l90eWtL8Y7x" | |
}, | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"id": "MFWB1pFEtuLa" | |
}, | |
"outputs": [], | |
"source": [ | |
"# zapis do pliku csv\n", | |
"import csv\n", | |
"\n", | |
"with open('test_data.csv', 'w', newline='') as file:\n", | |
" writer = csv.writer(file)\n", | |
" writer.writerow(['Name', 'Email', 'Phone Number'])\n", | |
"\n", | |
" for _ in range(10):\n", | |
" name = fake.name()\n", | |
" email = fake.email()\n", | |
" phone_number = fake.phone_number()\n", | |
" writer.writerow([name, email, phone_number])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Zapis danych testowych do pliku json" | |
], | |
"metadata": { | |
"id": "v6YOh7Zbt21f" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# zapis do pliku json\n", | |
"import json\n", | |
"\n", | |
"data = []\n", | |
"\n", | |
"for _ in range(10):\n", | |
" name = fake.name()\n", | |
" email = fake.email()\n", | |
" phone_number = fake.phone_number()\n", | |
" data.append({\n", | |
" 'name': name,\n", | |
" 'email': email,\n", | |
" 'phone_number': phone_number\n", | |
" })\n", | |
" with open('test_data.json', 'w') as file:\n", | |
" json.dump(data, file)" | |
], | |
"metadata": { | |
"id": "x5XJgIPRcuF7" | |
}, | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Zapis danych testowych do bazy SQLite" | |
], | |
"metadata": { | |
"id": "eGwPu_Zl83ks" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# zapis do bazy SQLite\n", | |
"import sqlite3\n", | |
"\n", | |
"# Utwórz połączenie z bazą danych SQLite i kursor\n", | |
"conn = sqlite3.connect('test_data.db')\n", | |
"cursor = conn.cursor()\n", | |
"\n", | |
"# Utwórz tabelę, jeżeli nie istnieje\n", | |
"cursor.execute('''\n", | |
"CREATE TABLE IF NOT EXISTS test_data (\n", | |
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n", | |
" name TEXT,\n", | |
" email TEXT,\n", | |
" phone_number TEXT\n", | |
");\n", | |
"''')\n", | |
"conn.commit()\n", | |
"\n", | |
"# Generuj dane i dodaj je do bazy danych\n", | |
"for _ in range(10):\n", | |
" name = fake.name()\n", | |
" email = fake.email()\n", | |
" phone_number = fake.phone_number()\n", | |
"\n", | |
" cursor.execute(\"INSERT INTO test_data (name, email, phone_number) VALUES (?, ?, ?)\", (name, email, phone_number))\n", | |
" conn.commit()\n", | |
"\n", | |
"# Zamknij połączenie z bazą danych\n", | |
"conn.close()\n" | |
], | |
"metadata": { | |
"id": "F3778wu0wEEh" | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment