[<InMemoryUploadedFile: sku_data_20240109123145.csv (text/csv)>, <InMemoryUploadedFile: sku_data_20240109123146.csv (text/csv)>] Detected encoding: Windows-1254, Confidence: 0.9856597913264844 Error: I/O operation on closed file. Detected encoding: SHIFT_JIS, Confidence: 0.99 Error: I/O operation on closed file.
エラーが続いているようですね。エラーが発生する原因として、ファイルの内容を読み込む段階で何かしらの問題が発生している可能性があります。
以下の点を確認してみてください:
-
ファイルの形式がCSVであることを確認: エラーメッセージに表示されているファイルが本当にCSV形式のファイルであるか確認してください。また、それ以外の形式のファイルが混ざっていないかも確認してください。
-
ファイルが正しくアップロードされているか確認: ファイルが正常にアップロードされているかどうか確認してください。特にファイルサイズがゼロでないことを確認してください。
-
ファイルを開く際のエラーハンドリングを強化: ファイルを開く際のエラーハンドリングを強化し、具体的なエラーメッセージを表示することで問題の特定を試みてください。
以下は修正案です:
python
import chardet import pandas as pd # ...(他のimportなど) data_list = [] # データを格納するリスト for file in files: try: with file.open('rb') as f: # ファイルの中身を一部読み取ってエンコーディングを判定 result = chardet.detect(f.read(10000)) detected_encoding = result['encoding'] confidence = result['confidence'] print(f"Detected encoding: {detected_encoding}, Confidence: {confidence}") # ファイルを再度開いてから読み込み with file.open('rb') as f: # ファイルがCSVかどうかのチェックも追加 if file.name.endswith('.csv'): try: df = pd.read_csv(f, encoding=detected_encoding) data_list.append(df) except UnicodeDecodeError as e: print(f"UnicodeDecodeError: {e}") print("Failed to read CSV file even with detected encoding.") except pd.errors.EmptyDataError: print("CSV file is empty.") else: print(f"Unsupported file format: {file.name}") except Exception as e: print(f"Error: {e}") # データが1つ以上ある場合にのみ pd.concat を実行 if data_list: df = pd.concat(data_list, axis=0) # 以下、データフレームの後続の処理を行う(例:CSVに保存、レスポンスとして返すなど) # 保存先のパスを指定(適切なパスを設定してください) output_path = "/Concat_data.csv" # ファイルの保存 df.to_csv(output_path, index=False, encoding="cp932", errors="ignore") # ファイルのダウンロードを促すレスポンスを返す with open(output_path, 'rb') as file: response = HttpResponse(file.read(), content_type='application/force-download') response['Content-Disposition'] = f'attachment; filename={os.path.basename(output_path)}' return response else: print("No valid data to concatenate.")
上記の修正でエラーの詳細が表示されるようになります。エラーメッセージを確認して、問題の特定に役立ててください。

ディスカッション
コメント一覧
まだ、コメントがありません