python chardetでエンコーディング(encoding)を判定する

課題 with open("textfile.txt", "r", encoding="sjis") as f: tmp = f.read() UnicodeEncodeError: 'shift_jis' codec can't encode character '\uff5e' in position 4807: illegal multibyte sequence というエラーが出て読み込みに失敗する場合がある。 原因 encoding の指定が適切でない。 解決 chardetでエンコーディングを判定する pythonの chardet というパッケージを使うとバイト列から文字コードを推定することができる。 まずはインストール。 python -m pip install chardet とりあえずバイトデータのまま読み込んで chardet で解析。 with open("textfile.txt", "rb") as f: bytestring = f.read() # バイト文字列 result = chardet.detect(bytestring) resultは {'encoding': 'utf-8', 'confidence': 0.99, 'language': ''} で99%の確率で utf-8 ということがわかる。 そうしたらバイト列をデコードする string = bytestring.decode(result["encoding"]) これで元の文字列を得ることができる。 文字コード この問題はoutlookの ....

April 15, 2025 · K

datatables ordering設定

ordering https://datatables.net/reference/option/ordering const table = $("#tableId").DataTable({ columns: [ { name: "link", data: "data_id", orderable: false, render: function(data, type, row, meta) { return '<a href="/{{request.blueprint}}/' + data + '/edit">Edit</a>'; } }, { name: "name", data: "name", title: "Name", }, { name: "position", data: "position", title: "Position", }, { name: "salary", data: "salary", title: "Salary", }, { name: "start_date", data: "start_date", title: "Start date", }, { name: "office", data: "office", title: "Office", }, { name: "extn", data: "extn", title: "Extn", }, ], ordering: true, }) デフォルト条件の設定 https://datatables....

March 17, 2025 · K

ubuntuでパーティションをマウントする

対象パーティションのUUIDを確認 sudo blkid /dev/nvme0n1p5: BLOCK_SIZE="512" UUID="F4F29332F292F856" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="844fb654-a7bf-44dd-affe-7fc5286b4b59" マウント先のディレクトリを作る sudo mkdir -p /mnt/data fstabにUUIDを追加して自動マウント設定をする sudo nano /etc/fstab UUID=F4F29332F292F856 /mnt/data ntfs defaults,nofail,uid=1000,gid=1000 0 0 を加える。 (必要なら)アンマウントする /media にマウントされている場合はアンマウントする必要がある。 sudo umount /media/usr/F4F29332F292F856 ターゲットがビジーのエラーが表示される場合 fuser コマンドでプロセスを確認 sudo fuser -m /media/usr/F4F29332F292F856 プロセスをキルする sudo kill -9 <processID> アンマウントする マウントする sudo mount -a fstabの変更を反映させる systemd を再読込 sudo systemctl daemon-reload エクスプローラーで開く アドレスバーに /mnt/data を入力

March 15, 2025 · K

datatablesのselect API

Download時にSELECTも含めておく。 初期化時に $("#table").DataTable({ select: true }) これは1つだけ選択する場合。 multi にすると複数選択できるようになる。 serverSideを使用しているときはrowIdも設定する データをサーバーから取ってくるようにしている場合は主キーに該当する列をrowIdに設定する必要がある。 $("#table").DataTable({ select: true, rowId: function(row) { return row["key"]; } }) これがないとリロード時にselectがリセットされてしまう。 (https://datatables.net/extensions/select/examples/initialisation/server-side-processing.html) 選択されたすべてのデータを取得する cumulative selectedIds = $("#table_id").DataTable().select.cumulative(); rowId のリストを取得できる event イベントも使用可能で、 // アイテムが選択された時 table.on('select', function (e, dt, type, indexes) { console.log('Items to be selected are now: ', type, indexes); if (type == "row") { var data = dt.rows(indexes).data().toArray(); console.log(data) } }); // アイテムが選択解除された時 table.on('deselect', function (e, dt, type, indexes) { console....

March 10, 2025 · K

datatablesのcolumnsオプション

例 const table = $("#tableId").DataTable({ columns: [ { name: "link", data: "data_id", orderable: false, render: function(data, type, row, meta) { return '<a href="/{{request.blueprint}}/' + data + '/edit">Edit</a>'; } }, { name: "name", data: "name", title: "Name", }, { name: "position", data: "position", title: "Position", }, { name: "salary", data: "salary", title: "Salary", }, { name: "start_date", data: "start_date", title: "Start date", }, { name: "office", data: "office", title: "Office", }, { name: "extn", data: "extn", title: "Extn", }, ], }) name 列の名前。普通は...

March 10, 2025 · K

datatablesのpaging処理

const table = $("#tableId").DataTable({ columns: [ { name: "link", data: "data_id", orderable: false, render: function(data, type, row, meta) { return '<a href="/{{request.blueprint}}/' + data + '/edit">Edit</a>'; } }, { name: "name", data: "name", title: "Name", }, { name: "position", data: "position", title: "Position", }, { name: "salary", data: "salary", title: "Salary", }, { name: "start_date", data: "start_date", title: "Start date", }, { name: "office", data: "office", title: "Office", }, { name: "extn", data: "extn", title: "Extn", }, ], paging: true, pageLength: 25, lengthMenu: [10, 25, 50, { label: 'All', value: -1 }] }) paging false の場合はすべてのデータがいっぺんに表示されるのでデータが多い場合は重くなってしまう。ただ、serverSide処理をしない限りは表示しようがしなかろうがすべてのデータを読み込むので重い場合はserverSideで処理すること。...

March 10, 2025 · K

datatablesのイベント処理

https://datatables.net/reference/event/?extn=autofill draw.dt https://datatables.net/reference/event/draw これはDataTablesが描画を完了したあとに実行される。 table.on('draw.dt', function() { var columns = table.columns().data(); var rows = table.rows().data(); }); functionは e と settings という引数を取れる。 settings というのは現在の DataTables の設定が読み込み専用の形ですべて登録されているオブジェクト(https://datatables.net/reference/type/DataTables.Settings)。 $.fn.dataTable.Api( selector ) で初期化(インスタンス化?)できる。 table.on('draw.dt', function(e, settings) { // initialize var api = new $.fn.dataTable.Api( settings ); const filteredData = api.rows( {search: "applied"} ).data().toArray(); if (settings._iRecordsTotal == filteredData.length) { console.log("フィルターはかかっていません"); } else { console.log("フィルターがかかっています"); } });

March 9, 2025 · K

datatablesにデータを渡す

html <table id="tableId" class="cell-border"></table> JSONの配列を渡す data = [ { "name": "Tiger Nixon", "position": "System Architect", "salary": "$320,800", "start_date": "2011/04/25", "office": "Edinburgh", "extn": "5421" }, ] $("#tableId").DataTable({ "data": data, columns: [ { name: "name", data: "name", title: "Name", }, { name: "position", data: "position", title: "Position", }, { name: "salary", data: "salary", title: "Salary", }, { name: "start_date", data: "start_date", title: "Start date", }, { name: "office", data: "office", title: "Office", }, { name: "extn", data: "extn", title: "Extn", }, ], }) flask render_template を使う場合 flaskから受け取るなら、...

March 9, 2025 · K

datatablesのserverSide処理

https://datatables.net/reference/option/serverSide 設定 初期化時に $("#tableId").DataTable({ serverSide: true, // <- これを追加 ajax: { url: "{{ url_for('.fetch_test') }}", type: "POST" // <- これを追加 }, columns: [ { name: "name", data: "name", title: "Name", }, { name: "position", data: "position", title: "position", }, { name: "salary", data: "salary", title: "salary", }, { name: "start_date", data: "start_date", title: "start_date", }, { name: "office", data: "office", title: "office", }, { name: "extn", data: "extn", title: "extn", }, ], }) serverSide: true を加える。 serverSide処理ではサーバーにデータテーブルのインフォメーションが送信されるから ajax の設定に type: "POST" も加える。...

March 9, 2025 · K

ubuntuを起動しようとしたらbusyboxが出てきた

UPDATEをずっと放ったらかしにしてしまっているなと思って数カ月ぶりに再起動したら起動できず、 busyboxというコンソール画面が出てきて先に進まずの状態に。 その時はChatGPTに聞いて対処できたのだがここにメモを残しておく ルートパーティションを探す lsblk このコマンドは私の環境では見つからなかった blkid TYPE="ext4" の可能性が高い。 sda数値 のどれか。 ルートパーティションをマウントする mount /dev/sdaX /root 。失敗したらファイルシステムが壊れているかもしれない ファイルシステムをチェックする fsck -y /dev/sdaX chrootで本来のシステムに入る mount -t proc /proc /root/proc mount -t sysfs /sys /root/sys mount --rbind /dev /root/dev chroot /root これが必要なのかはわからない initramfs を更新する update-initramfs -u -k all update-grub すべてアンマウントして再起動 chrootから出る exit アンマウント umount /root/proc umount /root/sys umount /root/dev umount /root device is busy と出てきたら -l オプションをつけて試す。 再起動 reboot その他 https://qiita.com/takanemu/items/911f1943ecaf764e973d に書いてあるが、 fsck -y /dev/sda1 reboot だけで直るかもしれない。...

February 27, 2025 · K