!wget https://github.com/tendo-sms/python_beginner_2023/raw/main/files_6/meta.txt !wget https://github.com/tendo-sms/python_beginner_2023/raw/main/files_6/data1.csv !wget https://github.com/tendo-sms/python_beginner_2023/raw/main/files_6/data2.csv !wget https://github.com/tendo-sms/python_beginner_2023/raw/main/files_6/data3.CSV !wget https://github.com/tendo-sms/python_beginner_2023/raw/main/files_6/sample.json # 読み込むファイル名を指定 変更したい場合は書き直す input_file = "meta.txt" # ファイルを読み込む with open(input_file, "r") as f: data = f.read() # データの表示 print(data) from pathlib import Path # 現在のディレクトリを取得 cwd = Path.cwd() # globを使ってパターンをワイルドカードの*にして全てを対象にする for f in cwd.glob("*"): # ファイル名を表示 print(f.name) # エラーが出る with open(f, "r") as f: print(f.read()) from pathlib import Path # 現在のディレクトリを取得 cwd = Path.cwd() for f in cwd.glob("*.csv"): # ファイル名を表示 print(f.name) with open(f, "r") as f: print(f.read()) from pathlib import Path # 現在のディレクトリを取得 cwd = Path.cwd() # srotedを追加してソートする for f in sorted(cwd.glob("*.csv")): # ファイル名を表示 print(f.name) with open(f, "r") as f: print(f.read()) # ディレクトリの一覧を確認 !ls from pathlib import Path # 現在のディレクトリを取得 cwd = Path.cwd() # .csvでも.CSVでも.Csvでも・・・処理対象にする for f in sorted(cwd.glob("*.[Cc][Ss][Vv]")): # ファイル名を表示 print(f.name) with open(f, "r") as f: print(f.read()) !pip install chardet import chardet from pathlib import Path # 現在のディレクトリを取得 cwd = Path.cwd() # .csvでも.CSVでも.Csvでも・・・処理対象にする for f in sorted(cwd.glob("*.[Cc][Ss][Vv]")): # エンコーディングを取得 enc = chardet.detect(open(f, "rb").read())["encoding"] # ファイル名とエンコーディングを表示 print(f"{f.name}のファイルのエンコーディング: {enc}") with open(f, "r", encoding=enc) as f: print(f.read()) from datetime import datetime # 年.月.日の場合 date1 = "2021.05.29" print(f"{date1} -> {datetime.strptime(date1, '%Y.%m.%d')}") # 年/月/日の場合 date2 = "2021/05/29" print(f"{date2} -> {datetime.strptime(date2, '%Y/%m/%d')}") from dateutil import parser # 年.月.日の場合 date1 = "2021.05.29" print(f"{date1} -> {parser.parse(date1)}") # 年/月/日の場合 date2 = "2021/05/29" print(f"{date2} -> {parser.parse(date2)}") from dateutil import parser date_lst = [ "2021.05.29", "2021-05-29T14:17:27Z", "2021-05-29 15:17:27.133860", "2021-05-29 16:17:27.133860+00:00", "2021-05-29 17:17:27.133860+05:00", "2021/05/29", "2021/05/29T14:17:27Z", "2021/05/29 15:17:27.133860", "2021/05/29 16:17:27.133860+00:00", "2021/05/29 17:17:27.133860+05:00", "May 29 2022 12:17PM", "May 29 2022 at 9:17PM", "May 29, 2022, 19:17:27", "Tue, 05-29-2022, 1:17AM", "Tue, 29 May, 2022", "Tuesday, 29th May, 2022 at 12:17p", "2022/02/ 7 14:04:45" ] for d in date_lst: print(f"{d} -> {parser.parse(d)}") from dateutil import parser date = "2022/02/ 7 14:04:45" # 文字列を修正 2022/02/ 7 14:04:45 -> 2022/02/07 14:04:45 new_date = date.replace("/ ", "/0") print(f"{date} -> {new_date} -> {parser.parse(new_date)}") import json # 読み込むファイル名を指定 input_file = "sample.json" # ファイルを読み込む with open(input_file, "r") as f: data = json.load(f) # データの表示 print(data) print(type(data)) # 辞書にデータを追加 data["あいうえお"] = "かきくけこ" # JSONで出力 with open("new_sample.json", "w") as f: json.dump(data, f) # 辞書にデータを追加 data["あいうえお"] = "かきくけこ" # JSONで出力 オプションindentとensure_asciiを追加 with open("new_sample.json", "w") as f: json.dump(data, f, indent=4, ensure_ascii=False) # シェルコマンドでテキストの中身を表示 !cat meta.txt # 読み込むファイル名を指定 変更したい場合は書き直す input_file = "meta.txt" # ファイルを読み込む with open(input_file, "r") as f: data = f.readlines() # ループで行ごとに調査する for d in data: # もし説明という文字が入っていたら表示する if "説明" in d: print(d.rstrip()) # 読み込むファイル名を指定 変更したい場合は書き直す input_file = "meta.txt" # ファイルを読み込む with open(input_file, "r") as f: data = f.readlines() # ループで行ごとに調査する for d in data: # もし説明という文字から開始していたら表示する if d.startswith("説明"): print(d.rstrip())