如何在 python Flask 中对两个或多个 CSV 文件进行验证

拉胡尔·辛格

请在不喜欢这个问题之前问我你不明白的地方大家好,我有数据生成程序,它会做很多计算,所以我不能在这里粘贴我的整个程序,所以只谈论我的程序 程序的所有计算都从阅读文件,所以当我在“选择文件”选项的网页中选择多个 CSV 文件时,我需要验证所有 csv 文件的列号(应该相同),列标题名称也应该匹配..我编写的程序是像这样:

from flask import Flask, render_template
import os
import csv
import pandas as pd
import numpy as np
app = Flask(__name__)

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def index():
    print("Loading the root file")
    return render_template("upload.html")
@app.route("/upload", methods=['POST'])
def upload():
    target = os.path.join(APP_ROOT, 'input/')
        print("target-",target)

        if not os.path.isdir(target):
            os.mkdir(target)

    for file in request.files.getlist("source_fileName"):
            print("file-",file)
            filename = file.filename
            print("filename-",filename)

            destination = "/".join([target, filename])
            print("destination-",destination)
            file.save(destination)
            print("file>",file)
            global tempFile
            tempFile = destination
            print("tempFile - " + tempFile)
    return redirect("/compute", )
def compute():
    readerForRowCheck = pd.read_csv(tempFile)
        for row in readerForRowCheck:
            if (len(row) != 8):
                return render_template("Incomplete.html")

            headerColumn1 = row[0];
            headerColumn2 = row[1];
            headerColumn3 = row[2];
            headerColumn4 = row[3];
            headerColumn5 = row[4];
            headerColumn6 = row[5];
            headerColumn7 = row[6];
            headerColumn8 = row[7];

            if (headerColumn1 != "Asset_Id") or (headerColumn2 != "Asset Family") \
                or (headerColumn3 != "Asset Name") or (headerColumn4 != "Location")or (headerColumn5 != "Asset Component") \
                or (headerColumn6 != "Keywords") or (headerColumn7 != "Conditions") or (headerColumn8 != "Parts") :
                    return render_template("incomplete.html")
.....................................so on to then it will go to perform other task

HTML程序:

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title> upload </title>
</head>
<body>
<div class="container">
    <h1>Large Data Generation</h1> 
<form id = "upload-form" action="{{ url_for('upload') }}" method="POST" enctype="multipart/form-data">
        <div id="file-selector">
            <p> 
                <strong>Source File: </strong>
                <input id="source_fileName" type="file" name="source_fileName" accept="csv/*" multiple />
            </p> 
        </div>
    <input type="submit" value="Generate Data" id="upload-button"  >
</form>
</div>
</body>

注意:**我只给出了重要的代码行,否则它包含大量代码**在这里我知道我应该如何验证列号和名称上的 csv 文件应该相同我知道我对读取 csv 的验证文件不正确,为什么我在这里请帮助我.....谢谢

高拉夫

您有多个文件,那么您需要为每个文件创建数据帧实例上传功能将如下所示:

def upload():
    target = os.path.join(APP_ROOT, 'input/')
        print("target-",target)
        if not os.path.isdir(target):
            os.mkdir(target)
    abs_path_files=[]
    for file in request.files.getlist("source_fileName"):
            print("file-",file)
            filename = file.filename
            print("filename-",filename)
            destination = "/".join([target, filename])
            print("destination-",destination)
            file.save(destination)
            print("file>",file)
            tempFile = os.path.abspath(destination)
            abs_path_files.append(tempfile)
            print("tempFile - " + tempFile)
    return redirect(url_for("compute", files_list=abs_path_files))

计算函数将如下所示:

def compute(files_list):
    dataFrames=[]
    for f in files_list:
        dataFrame=pd.read_csv(f)
        dataFrames.append(dataFrame)
    col_in_files = set([",".join(list(f.column.values)) for f in dataFrames])
    if len(col_in_files)==1:
       #then process your data here

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在python中合并两个csv文件

来自分类Dev

如何在JavaScript中合并两个.csv文件?

来自分类Dev

如何在空手道中的单个功能文件中使用两个或多个csv文件?

来自分类Dev

如何在两个大的csv文件中找到字符串中的子字符串(Python)

来自分类Dev

如何在python中对csv文件中的范围进行排序?

来自分类Dev

如何验证两个不同的.csv文件列ID与python匹配?

来自分类Dev

如何根据Python列中的值将csv文件分为两个文件?

来自分类Dev

如何比较两个csv文件并使用python在新文件中写入1或0

来自分类Dev

如何在python的csv文件中使用Unicode字符串保存两个列表?

来自分类Dev

如何将csv中的两列与之前在python中声明的两个值进行比较?

来自分类Dev

如何使用Python对两个.CSV文件中的仅前3个位置进行排序和存储,然后将它们存储在一个.CSV文件中的两列中?

来自分类Dev

在Python中合并两个CSV文件

来自分类Dev

在python中交叉引用两个csv文件

来自分类Dev

如何在Python中编写.csv文件?

来自分类Dev

如何在python中枚举.csv文件?

来自分类Dev

如何在Python中删除CSV文件

来自分类Dev

如何在python中解析csv文件

来自分类Dev

Python:如何检查文本文件中是否存在两个或多个给定的单词

来自分类Dev

我如何从python中的两个列表中创建包含多个子文件夹的多个文件夹

来自分类Dev

如何在机器人框架中读取csv文件以进行数据验证

来自分类Dev

如何在Windows中比较两个csv文件

来自分类Dev

如何在Windows中基于Colum值将csv文件分为两个文件?

来自分类Dev

如何比较R中的两个csv文件?

来自分类Dev

如何比较 CSV 文件中的两个表?

来自分类Dev

Python:如何比较两个csv文件并在新文件中打印出匹配的字符串

来自分类Dev

如何在CSV文件中查找任何两个元素的共享属性

来自分类Dev

如何在excel csv文件中添加两个表?

来自分类Dev

根据python中的两列在python中映射或合并两个csv文件?

来自分类Dev

如何在QuerySelectField中获得两个标签-Flask

Related 相关文章

  1. 1

    如何在python中合并两个csv文件

  2. 2

    如何在JavaScript中合并两个.csv文件?

  3. 3

    如何在空手道中的单个功能文件中使用两个或多个csv文件?

  4. 4

    如何在两个大的csv文件中找到字符串中的子字符串(Python)

  5. 5

    如何在python中对csv文件中的范围进行排序?

  6. 6

    如何验证两个不同的.csv文件列ID与python匹配?

  7. 7

    如何根据Python列中的值将csv文件分为两个文件?

  8. 8

    如何比较两个csv文件并使用python在新文件中写入1或0

  9. 9

    如何在python的csv文件中使用Unicode字符串保存两个列表?

  10. 10

    如何将csv中的两列与之前在python中声明的两个值进行比较?

  11. 11

    如何使用Python对两个.CSV文件中的仅前3个位置进行排序和存储,然后将它们存储在一个.CSV文件中的两列中?

  12. 12

    在Python中合并两个CSV文件

  13. 13

    在python中交叉引用两个csv文件

  14. 14

    如何在Python中编写.csv文件?

  15. 15

    如何在python中枚举.csv文件?

  16. 16

    如何在Python中删除CSV文件

  17. 17

    如何在python中解析csv文件

  18. 18

    Python:如何检查文本文件中是否存在两个或多个给定的单词

  19. 19

    我如何从python中的两个列表中创建包含多个子文件夹的多个文件夹

  20. 20

    如何在机器人框架中读取csv文件以进行数据验证

  21. 21

    如何在Windows中比较两个csv文件

  22. 22

    如何在Windows中基于Colum值将csv文件分为两个文件?

  23. 23

    如何比较R中的两个csv文件?

  24. 24

    如何比较 CSV 文件中的两个表?

  25. 25

    Python:如何比较两个csv文件并在新文件中打印出匹配的字符串

  26. 26

    如何在CSV文件中查找任何两个元素的共享属性

  27. 27

    如何在excel csv文件中添加两个表?

  28. 28

    根据python中的两列在python中映射或合并两个csv文件?

  29. 29

    如何在QuerySelectField中获得两个标签-Flask

热门标签

归档