Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 356313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:05:59+00:00 2026-05-12T12:05:59+00:00

I am expecting users to upload a CSV file of max size 1MB to

  • 0

I am expecting users to upload a CSV file of max size 1MB to a web form that should fit a given format similar to:

"<String>","<String>",<Int>,<Float>

That will be processed later. I would like to verify the file fits a specified format so that the program that shall later use the file doesnt receive unexpected input and that there are no security concerns (say some injection attack against the parsing script that does some calculations and db insert).

(1) What would be the best way to go about doing this that would be fast and thorough? From what I’ve researched I could go the path of regex or something more like this. I’ve looked at the python csv module but that doesnt appear to have any built in verification.

(2) Assuming I go for a regex, can anyone direct me to towards the best way to do this? Do I match for illegal characters and reject on that? (eg. no ‘/’ ‘\’ ‘<‘ ‘>’ ‘{‘ ‘}’ etc.) or match on all legal eg. [a-zA-Z0-9]{1,10} for the string component? I’m not too familiar with regular expressions so pointers or examples would be appreciated.

EDIT:
Strings should contain no commas or quotes it would just contain a name (ie. first name, last name). And yes I forgot to add they would be double quoted.

EDIT #2:
Thanks for all the answers. Cutplace is quite interesting but is a standalone. Decided to go with pyparsing in the end because it gives more flexibility should I add more formats.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:06:00+00:00Added an answer on May 12, 2026 at 12:06 pm

    Pyparsing will process this data, and will be tolerant of unexpected things like spaces before and after commas, commas within quotes, etc. (csv module is too, but regex solutions force you to add “\s*” bits all over the place).

    from pyparsing import *
    
    integer = Regex(r"-?\d+").setName("integer")
    integer.setParseAction(lambda tokens: int(tokens[0]))
    floatnum = Regex(r"-?\d+\.\d*").setName("float")
    floatnum.setParseAction(lambda tokens: float(tokens[0]))
    dblQuotedString.setParseAction(removeQuotes)
    COMMA = Suppress(',')
    validLine = dblQuotedString + COMMA + dblQuotedString + COMMA + \
            integer + COMMA + floatnum + LineEnd()
    
    tests = """\
    "good data","good2",100,3.14
    "good data" , "good2", 100, 3.14
    bad, "good","good2",100,3.14
    "bad","good2",100,3
    "bad","good2",100.5,3
    """.splitlines()
    
    for t in tests:
        print t
        try:
            print validLine.parseString(t).asList()
        except ParseException, pe:
            print pe.markInputline('?')
            print pe.msg
        print
    

    Prints

    "good data","good2",100,3.14
    ['good data', 'good2', 100, 3.1400000000000001]
    
    "good data" , "good2", 100, 3.14
    ['good data', 'good2', 100, 3.1400000000000001]
    
    bad, "good","good2",100,3.14
    ?bad, "good","good2",100,3.14
    Expected string enclosed in double quotes
    
    "bad","good2",100,3
    "bad","good2",100,?3
    Expected float
    
    "bad","good2",100.5,3
    "bad","good2",100?.5,3
    Expected ","
    

    You will probably be stripping those quotation marks off at some future time, pyparsing can do that at parse time by adding:

    dblQuotedString.setParseAction(removeQuotes)
    

    If you want to add comment support to your input file, say a ‘#’ followed by the rest of the line, you can do this:

    comment = '#' + restOfline
    validLine.ignore(comment)
    

    You can also add names to these fields, so that you can access them by name instead of index position (which I find gives more robust code in light of changes down the road):

    validLine = dblQuotedString("key") + COMMA + dblQuotedString("title") + COMMA + \
            integer("qty") + COMMA + floatnum("price") + LineEnd()
    

    And your post-processing code can then do this:

    data = validLine.parseString(t)
    print "%(key)s: %(title)s, %(qty)d in stock at $%(price).2f" % data
    print data.qty*data.price
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an system with which users can upload a CSV file via an
I would like to display the size of the file that was chosen via
I'm adding some functionality to my site so that users can upload their own
I have a upload folder where users can upload anything, all file types are
In a web application I have an upload folder where all users' files are
I'm building a web tool which allows users to upload PDFs to a server
[edit] We're collecting credit application data from users on a web form. I have
I'm building a django based database app that allows users to upload supporting documentation.
I am building a web application that allows a user to upload an image.
I always felt that expecting exceptions to be thrown on a regular basis and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.