Python– category –
-
【Python】配列の重複している要素を削除する
Python
number = [1, 2, 3, 4, 5, 5, 6] print(list(set(number))) # [1, 2, 3, 4, 5, 6] 参考:https://docs.python.org/ja/3/c-api/set.html -
【Python】配列を比較し含まれていない要素を抽出
Python
number = [1, 2, 3, 4, 5] odd = [1, 3, 5] even = [2, 4] print(list(set(number) - set(odd))) # [2, 4] print(list(set(number) - set(even))) # [1, 3, 5] print(list(set(odd) - set(even))) # [1, 3, 5] 参考:https://docs... -
Python type XXX cannot be converted
Python
mysql.connector.errors.InterfaceError: Failed executing the operation; Python type Tag cannot be converted BeautifulSoupを使用して取得したHTMLタグをそのままMySQLにデータ挿入しようとした時に表示されたエラー。 soup.find(class_="xxx") # Tag... -
【BeautifulSoup】特定のname属性値を持つ要素を取得する
Python
soup = BeautifulSoup(html, 'html.parser') select = soup.find('select', attrs={"name": "products"}) 参考:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attrs -
【Python】日付のフォーマット
Python
from datetime import datetime from datetime import timedelta dt = datetime.strptime('2024年' + date, '%Y年%m月%d日') // 文字列から日付へ変更 str = dt.strftime('%Y-%m-%d') // 日付から文字列へフォーマット -
【Python】DBの返り値
Python
for c in cur: print(c) // (column1, column2, column3) for (column1, column2, column3) in cur: print(column1) print(column2) print(column3) -
【Python】AttributeError: ‘str’ object has no attribute ‘read’
Python
json.load(str) // AttributeError: 'str' object has no attribute 'read' json.loads(str) // 文字列に対して利用 -
BeautifulSoupで不要な文字列を削除する(decompose)
Python
<h2>wanted<span class="aaa">www</span></h2> h2 = soup.find('h2') span = soup.find('span') span.decompose() print(h2.text) // wanted h2 = soup.find('h2').text span = soup.find('span') span.decompose() print(h2) // wanted www -
【Python】requestのヘッダーを設定する
Python
headers = { 'aaa': 'bbb', 'ccc': 'ddd' } response = requests.get(url, header) -
【Python】日付フォーマットで0埋めしない方法
Python
date = datetime.strftime("%Y年%-m月%-d日") // 「-」を追加する(MacOS)