Fixing “Failed to Load Module Canberra-GTK-Module” Error

#debian
sudo apt update
sudo apt install libcanberra-gtk-module libcanberra-gtk3-module -y

#redhat
sudo dnf makecache
sudo dnf -y install libcanberra-gtk3

# Arch-based Linux distros
sudo pacman -Sy libcanberra

發表於 未分類 | 已標籤 | 發表迴響

python-pandas无法打开.xlsx文件,xlrd.biffh.XLRDError: Excel xlsx file; not supported

原因是最近xlrd更新到了2.0.1版本,只支持.xls文件。所以pandas.read_excel(‘xxx.xlsx’)会报错。

可以安装旧版xlrd,在cmd中运行:

pip uninstall xlrd
pip install xlrd==1.2.0

也可以用openpyxl代替xlrd打开.xlsx文件:

df=pandas.read_excel(‘data.xlsx’,engine=‘openpyxl’)

發表於 python | 發表迴響

python 列表推導式

ex1:
list1 = [1,2,3,4,5,6]
list2 = []
for num in list1
list2.append(num*num)

更為簡單寫法
list1 = [1,2,3,4,5,6]
list2 = [num**2 for num in list1]

發表於 python | 發表迴響

python qt icon

#https://www.byhy.net/tut/py/gui/qt_04/

from PySide2.QtGui import QIcon

app = QApplication([])

#import icon
app.setWindowIcon(QIcon(‘logo.png’))

#png to ico

https://www.zamzar.com/convert/png-to-ico/

https://www.easyicon.net/convert/

發表於 python | 發表迴響

python read readline readlines 差別

read() : 讀取整個文件
程式範例:
with open(‘test.txt’) as f:
test_data = f.read()
print( test_data )

會讀取整個文件

readline() : 讀取下一行
程式範例:
with open(‘test.txt’) as f:
test_data = f.readline()
print( test_data )

讀取一行

readlines() : 讀取整個文件,遍歷
程式範例:
with open(‘test.txt’) as f:
test_data = f.readlines()
print( test_data )

所以字串變成 list

發表於 python | 發表迴響

python 裝飾器

def 裝飾器 (callback):
def 內部function():
裝飾器程式碼
callback()
return 內部function

使用裝飾器
@裝飾器
def function():
#function 程式碼
function() #呼叫帶裝飾器函式

#範例
import time

def sayLocal(func):
def wrapper(*args,**kargs):
curTime = func(*args,**kargs)
return f’当地时间: {curTime}’
return wrapper

@sayLocal
def getXXXTimeFormat1(name):
curTime = time.strftime(‘%Y-%m-%d %H:%M:%S’,time.localtime())
return f’{curTime} ,数据采集者:{name} ‘

@sayLocal
def getXXXTimeFormat2(name,place):
curTime = time.strftime(‘%Y-%m-%d %H:%M:%S’,time.localtime())
return f’{curTime} ,数据采集者:{name} , 采集地:{place}’

print (getXXXTimeFormat1(‘张三’))
print (getXXXTimeFormat2(‘张三’,place=’北京’))

發表於 python | 發表迴響

linux check usb disk

linux 對usb 檢查 bad sector
badblocks -w -s -o error.log /dev/sdx

-w :使用寫模式測試通過在設備的每個塊上寫入一些模式,讀取每個塊並比較內容來掃描壞塊
-s 顯示掃描進度
-o error.log 就LOG
———————————————————
f3 是一個簡單的工具,可以測試閃存卡的容量和性能
apt install f3 或
wget https://github.com/AltraMayor/f3/archive/refs/tags/v8.0.tar.gz
make
make install

mount /dev/sdx /mnt/a

寫入測試
f3write /mnt/a

讀取測試
frread/mnt/a

——————————-
簡易容量測試 #–destructive 這命令會刪除資料
sudo ./f3probe –destructive –time-ops /dev/sdb
————————————-
Linux 中將我的 USB 記憶棒的容量更正為實際大小,在使用 f3probe
sudo ./f3fix –last-sec=16477878 /dev/sdb

https://www.cyberciti.biz/faq/linux-check-the-physical-health-of-a-usb-stick-flash-drive/?fbclid=IwAR2bxQrXrg5TmDkzUUdbRKZqz6lE7xZ2lBZcAdrRuMEMhiMPdMugelZsOOQ

發表於 linux | 發表迴響

python 尋找數字

使用 re 模組從字串中提取數字
Python 的 re 模組還提供了可以搜尋字串並提取結果的函式。re 模組提供了 findall() 方法,該方法返回所有匹配結果的列表。下面給出一個示例程式碼。

import re

temp_string = "Hi my age is 32 years and 250.5 days12″
print(temp_string)
print([float(s) for s in re.findall(r'-?\d+\.?\d*', temp_string)])

https://www.delftstack.com/zh-tw/howto/python/python-extract-number-from-string/

發表於 未分類 | 發表迴響

python search txt 方式

https://www.delftstack.com/zh-tw/howto/python/python-find-string-in-file/#%e5%9c%a8-python-%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%aa%94%e6%a1%88-readlines-%e6%96%b9%e6%b3%95%e6%9f%a5%e8%a9%a2%e6%aa%94%e6%a1%88%e4%b8%ad%e7%9a%84%e5%ad%97%e4%b8%b2

https://github.com/dokelung/Python-QA/blob/master/questions/file/Python%E5%A6%82%E4%BD%95%E5%AF%A6%E7%8F%BE%E4%B8%A6%E8%A1%8C%E6%9F%A5%E6%89%BE%E9%97%9C%E9%8D%B5%E5%AD%97%E6%89%80%E5%9C%A8%E7%9A%84%E8%A1%8C.md

建議使用下列 不要用 readline()
with open(‘XXX’, ‘r’) as f:
for line in f:
if ‘txt’ in line:
# do something…

發表於 python | 發表迴響

python 路徑 path 加入問題

#之前對變數加入問題,查到的解決方案

from pathlib import Path
nday = ’201201020.log’

#pathlib.Path()
palib = Path(‘c:’,'/’,'Users’, nday)
#print ( Path(‘c:’,'/’,'Users’) )
print ( palib )

#os.path()
import os
ospath = ( os.path.join(‘c:’, os.sep, ‘Users’, nday) )
print ( ospath )

或者是
flogfile = "D:\\Backup\\hsbat\\log\\" + nday + ‘.log’
print( flogfile )

發表於 python | 發表迴響