Skip to content

Instantly share code, notes, and snippets.

@nonZero
Created April 26, 2021 13:18
Show Gist options
  • Save nonZero/ecae90cafbee8d1c7a649be8566bf94d to your computer and use it in GitHub Desktop.
Save nonZero/ecae90cafbee8d1c7a649be8566bf94d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import string\n",
"import random\n",
"import numpy as np\n",
"import zipfile\n",
"import io"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 'y', 'k', 'd', 't', 'l']"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.sample(string.ascii_lowercase, 6)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[54, 7, 97, 48, 72, 34],\n",
" [56, 59, 52, 51, 90, 15],\n",
" [55, 13, 15, 46, 17, 98],\n",
" [23, 90, 71, 38, 33, 34],\n",
" [48, 61, 49, 19, 32, 32],\n",
" [49, 65, 33, 51, 28, 60],\n",
" [82, 36, 44, 91, 25, 53],\n",
" [51, 86, 62, 54, 17, 71],\n",
" [18, 76, 8, 22, 2, 42],\n",
" [72, 30, 39, 52, 86, 62]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randint(1,100, (10, 6))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def generate_a_df():\n",
" cols = random.sample(string.ascii_lowercase, random.randint(5,10))\n",
" n = random.randint(10,100)\n",
" return pd.DataFrame(np.random.randint(1, 100, (n, len(cols))), columns=cols) "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"dfs = {f\"df{i:04}\": generate_a_df() for i in range(15)}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"with zipfile.ZipFile(\"my.zip\", \"w\") as z:\n",
" for name, df in dfs.items():\n",
" buf = io.BytesIO()\n",
" df.to_csv(buf, index=False)\n",
" buf.seek(0)\n",
" z.writestr(f\"{name}.csv\", buf.read())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-rw-r--r-- 1 udi udi 20255 Apr 26 16:18 my.zip\n"
]
}
],
"source": [
"!ls -l my.zip"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Archive: my.zip\n",
" Length Date Time Name\n",
"--------- ---------- ----- ----\n",
" 1228 2021-04-26 16:18 df0000.csv\n",
" 588 2021-04-26 16:18 df0001.csv\n",
" 1679 2021-04-26 16:18 df0002.csv\n",
" 1886 2021-04-26 16:18 df0003.csv\n",
" 1627 2021-04-26 16:18 df0004.csv\n",
" 1783 2021-04-26 16:18 df0005.csv\n",
" 218 2021-04-26 16:18 df0006.csv\n",
" 455 2021-04-26 16:18 df0007.csv\n",
" 2805 2021-04-26 16:18 df0008.csv\n",
" 377 2021-04-26 16:18 df0009.csv\n",
" 950 2021-04-26 16:18 df0010.csv\n",
" 1565 2021-04-26 16:18 df0011.csv\n",
" 171 2021-04-26 16:18 df0012.csv\n",
" 1791 2021-04-26 16:18 df0013.csv\n",
" 1670 2021-04-26 16:18 df0014.csv\n",
"--------- -------\n",
" 18793 15 files\n"
]
}
],
"source": [
"!unzip -l my.zip"
]
},
{
"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.9.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment