{ "cells": [ { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "import pathlib\n", "import json" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list((pathlib.Path()/'srv1').iterdir())" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "scrolled": true }, "outputs": [], "source": [ "txtfiles = list((pathlib.Path()/'textfiles').iterdir())" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[WindowsPath('textfiles/transcript1-1.txt'),\n", " WindowsPath('textfiles/transcript2-1.txt'),\n", " WindowsPath('textfiles/transcript3-1.txt'),\n", " WindowsPath('textfiles/transcript4-1.txt'),\n", " WindowsPath('textfiles/transcript4-2.txt'),\n", " WindowsPath('textfiles/transcript5-1.txt'),\n", " WindowsPath('textfiles/transcript5-2.txt'),\n", " WindowsPath('textfiles/transcript5-3.txt'),\n", " WindowsPath('textfiles/transcript6-1.txt')]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "txtfiles" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "def make_json(txtfile):\n", " my_dict = {}\n", " with txtfile.open() as f:\n", " cnt = 0\n", " for line in f:\n", " if (cnt%3)==0:\n", " a = line\n", " a[0].split(',.')\n", " b = a.split(',')[0]\n", " dt = datetime.strptime(b.split('.')[0], '%H:%M:%S')\n", " tm = f'{dt.hour*60 + dt.minute:02d}' + \":\" + f'{dt.second:02d}'\n", " if (cnt%3)==1:\n", " txt = line.strip()\n", " if (cnt%3)==2:\n", " my_dict[tm] = txt\n", " cnt += 1\n", " json.dump(my_dict, txtfile.with_suffix('.json').open('w'), indent=2)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "for txtfile in txtfiles:\n", " make_json(txtfile)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "json.dump()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "a = '0:00:00.000,0:00:04.440\\n'" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'00:00'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0].split(',.')\n", "b = a.split(',')[0]\n", "dt = datetime.strptime(b.split('.')[0], '%H:%M:%S')\n", "f'{dt.hour + dt.minute:02d}' + \":\" + f'{dt.second:02d}'" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.minute" ] }, { "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 }