0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

for / ファイル読み込みフォルダ 書き方 / デバッグ / 関数作るか作らんかの違い/ smtplib と imaplib

Posted at
CSVやexcelファイル使うとき、よく使う
for row in reader:
range 仕組み
for i in range(1, 5):  
    print(f"{i}回目の処理")

    結果
    1回目の処理
2回目の処理
3回目の処理
4回目の処理
seleniumのみで、タブ切り替えるときよく使う
for i in range(1, len(driver.window_handles)):  
タブが3つあり ['タブ1', 'タブ2', 'タブ3'] な場合len(driver.window_handles) の中身は 3

使いたい : フォルダ数個とフォルダ一個

フォルダ数個
folder_path = ["/Users/chomechomesuke/Desktop/other/excel", "/Users/chomechomesuke/Desktop/other/kyujin", "/Users/chomechomesuke/Desktop/other/pdf"]
フォルダ1個だけ
folder_path = "/Users/chomechomesuke/Desktop/other/"

指定CSSないよ

原因 : html構造 変わった

スクリーンショット 2025-03-03 17.36.58.png

対処法
# 検索結果のリンクを取得(DuckDuckGoの検索結果の構造に基づく)
results = driver.find_elements(By.CSS_SELECTOR, "a.result__link")  

contentブロックとif / elseブロックセット

contentとif elseのインデントは、分ける

スクリーンショット 2025-03-10 19.14.21.png

スクリーンショット 2025-03-10 19.14.31.png


if /eise は、いつもインデント4個分

スクリーンショット 2025-03-10 17.13.05.png


ファイル、書き込み保存ちゃんとできてる?

import os

if os.access(json_file_path, os.W_OK):
    print("書き込み可能")
else:
    print("書き込み不可")

json_fileパスあってる?

mport os

json_file_path = "url.json"  # これが意図しない場所に作られるかも
absolute_path = os.path.abspath(json_file_path)

print(f"Pythonが作成したパス: {absolute_path}")

if os.path.exists(absolute_path):
    print("✅ ファイルが存在する")
else:
    print("❌ ファイルが見つからない")

データが渡されてるか確認 print (変数名)

data = []
for row in ws.iter_rows(min_row=2, values_only=True):  # 1行目がヘッダーならmin_row=2
    if all(cell is not None and cell != "" for cell in row):
        record = list(row)
    # record ={
        # "会社名": row[0],
    # }
    data.append(record)
    print(data)

関数を作る処理と、関数を作らん処理違い

関数作るver
   1. 何度も同じ処理させたい
   : ファイルの読み込み / 解析/データ加工の処理したい 
  
  2 . 同じ処理させたい変数が何個もある
  リストを2倍
  def double(x): 
    return x * 2 
    numbers = [1, 2, 3, 4] 
    doubled_numbers = [double(n) for n in numbers] 
    print(doubled_numbers) 
    
    結果 2 4 6 8
    (N : データがある個数分繰り返す)
関数いらんver
    一度しか使わん単純な処理 

smtplibとimaplib 違い

  smtplib : 送信専用 
  imaplib : 受信メール検索専用

for文 使い方例

指定条件データが何番目にあるか、調べたい

def linear_search(lst, target):
    for i in range(len(lst)):  # リストのすべての要素を調べる
        if lst[i] == target:  # 要素が目標と一致したら
            return i  # インデックスを返す
    return -1  # 見つからなかった場合、-1を返す

# 使用例
my_list = [10, 20, 30, 40, 50]
result = linear_search(my_list, 30)
print(result)  # 2(インデックス)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?