How to do validation for Two or more CSV file in python flask












-1















Please Before Dislike this Question ask me what you don't understand Hello Guys i have data generation program which will do lots of computation so i cannot paste my whole program here so only talking about my program All the computation of the program starts with reading the file so when i'm selecting multiple CSV file in web page in "choose file" option i need to validate the columns numbers(should be same) of the all csv file and columns headers name should also match.. program i have written is like this:



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 program:



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>


Note:**I have only given lines of code which important otherwise its contain lots of code **Here i'm getting that how should i validate the csv file on the columns numbers and name should be same i know my validation for reading csv file is not correct that why i'm here please Help me.....thanks










share|improve this question





























    -1















    Please Before Dislike this Question ask me what you don't understand Hello Guys i have data generation program which will do lots of computation so i cannot paste my whole program here so only talking about my program All the computation of the program starts with reading the file so when i'm selecting multiple CSV file in web page in "choose file" option i need to validate the columns numbers(should be same) of the all csv file and columns headers name should also match.. program i have written is like this:



    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 program:



    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>


    Note:**I have only given lines of code which important otherwise its contain lots of code **Here i'm getting that how should i validate the csv file on the columns numbers and name should be same i know my validation for reading csv file is not correct that why i'm here please Help me.....thanks










    share|improve this question



























      -1












      -1








      -1








      Please Before Dislike this Question ask me what you don't understand Hello Guys i have data generation program which will do lots of computation so i cannot paste my whole program here so only talking about my program All the computation of the program starts with reading the file so when i'm selecting multiple CSV file in web page in "choose file" option i need to validate the columns numbers(should be same) of the all csv file and columns headers name should also match.. program i have written is like this:



      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 program:



      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>


      Note:**I have only given lines of code which important otherwise its contain lots of code **Here i'm getting that how should i validate the csv file on the columns numbers and name should be same i know my validation for reading csv file is not correct that why i'm here please Help me.....thanks










      share|improve this question
















      Please Before Dislike this Question ask me what you don't understand Hello Guys i have data generation program which will do lots of computation so i cannot paste my whole program here so only talking about my program All the computation of the program starts with reading the file so when i'm selecting multiple CSV file in web page in "choose file" option i need to validate the columns numbers(should be same) of the all csv file and columns headers name should also match.. program i have written is like this:



      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 program:



      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>


      Note:**I have only given lines of code which important otherwise its contain lots of code **Here i'm getting that how should i validate the csv file on the columns numbers and name should be same i know my validation for reading csv file is not correct that why i'm here please Help me.....thanks







      python csv flask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 29 '18 at 12:00







      rahul singh

















      asked Dec 29 '18 at 6:27









      rahul singhrahul singh

      1048




      1048
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I You are having multiple files then you need to create instance of dataframe for each file
          Upload function will look like this:



          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))


          Compute Function will look like this:



          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





          share|improve this answer


























          • Comments are not for extended discussion; this conversation has been moved to chat.

            – Yvette Colomb
            Dec 29 '18 at 23:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967245%2fhow-to-do-validation-for-two-or-more-csv-file-in-python-flask%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I You are having multiple files then you need to create instance of dataframe for each file
          Upload function will look like this:



          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))


          Compute Function will look like this:



          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





          share|improve this answer


























          • Comments are not for extended discussion; this conversation has been moved to chat.

            – Yvette Colomb
            Dec 29 '18 at 23:52
















          1














          I You are having multiple files then you need to create instance of dataframe for each file
          Upload function will look like this:



          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))


          Compute Function will look like this:



          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





          share|improve this answer


























          • Comments are not for extended discussion; this conversation has been moved to chat.

            – Yvette Colomb
            Dec 29 '18 at 23:52














          1












          1








          1







          I You are having multiple files then you need to create instance of dataframe for each file
          Upload function will look like this:



          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))


          Compute Function will look like this:



          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





          share|improve this answer















          I You are having multiple files then you need to create instance of dataframe for each file
          Upload function will look like this:



          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))


          Compute Function will look like this:



          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 29 '18 at 11:24

























          answered Dec 29 '18 at 7:36









          GauravGaurav

          18010




          18010













          • Comments are not for extended discussion; this conversation has been moved to chat.

            – Yvette Colomb
            Dec 29 '18 at 23:52



















          • Comments are not for extended discussion; this conversation has been moved to chat.

            – Yvette Colomb
            Dec 29 '18 at 23:52

















          Comments are not for extended discussion; this conversation has been moved to chat.

          – Yvette Colomb
          Dec 29 '18 at 23:52





          Comments are not for extended discussion; this conversation has been moved to chat.

          – Yvette Colomb
          Dec 29 '18 at 23:52


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967245%2fhow-to-do-validation-for-two-or-more-csv-file-in-python-flask%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas