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?

こちらのシステムの実装に向けてモックアップを作成しました。
地方公共団体情報システム標準化の適合確認システム

ChatGPT

ChatGPT に次の依頼をします。

次のWeb システムを作成して下さい。
csv ファイルをアップロードし、ページに、コラム数と、列数を表示する。

ChatGPT の回答

ChatGPT は、Flask の例を提示しました。

フォルダー構造

$ tree
.
├── app.py
└── templates
    └── index.html
app.py
from flask import Flask, request, render_template
import csv
from io import TextIOWrapper

app = Flask(__name__)

# ホームページの表示
@app.route('/')
def index():
    return render_template('index.html')

# ファイルのアップロードと処理
@app.route('/upload', methods=['POST'])
def upload():
    if 'csvfile' not in request.files:
        return "ファイルがありません"

    csvfile = request.files['csvfile']
    if csvfile.filename == '':
        return "ファイルがありません"

    # CSVファイルを読み込み、列数と行数を取得
    try:
        csv_reader = csv.reader(TextIOWrapper(csvfile, 'utf-8'))
        header = next(csv_reader)  # ヘッダーを読み飛ばす
        num_columns = len(header)
        num_rows = sum(1 for row in csv_reader)
        
        return f"アップロードされたCSVファイルの列数: {num_columns}, 行数: {num_rows}"
    
    except Exception as e:
        return f"エラーが発生しました: {str(e)}"

if __name__ == '__main__':
    app.run(debug=True)
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSVファイルアップロード</title>
</head>
<body>
    <h2>CSVファイルをアップロードしてください</h2>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="csvfile" accept=".csv">
        <button type="submit">ファイルをアップロード</button>
    </form>
</body>
</html>

サーバーの実行

python app.py
$ python app.py
 * Serving Flask app 'app'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 999-058-055

クライアント

http://127.0.0.1:5000 にアクセス
image.png

次のファイルをアップロードします。

cities_header.csv
id,name,population,date_mod
t1271,千葉,59178,2003-9-21
t1272,勝浦,47215,2003-10-15
t1273,市原,83654,2003-6-14

実行結果
image.png

期待通りの結果が得られました。
ヘッダーのない CSV ファイルだと、列数が、(実際の列数 - 1) になります。

次のフェーズ

CSV ファイルが基準に適合するか否かを判断する関数を Python で記述する。

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?