PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How to parse csv into dictionary in Python without saving file?

 August 28, 2022     csv, flask, python     No comments   

Issue

CSV file uploaded by user and then I want to parse it to dictionary without actually saving the uploaded file.

I found this reference and tried it https://riptutorial.com/flask/example/32038/parse-csv-file-upload-as-list-of-dictionaries-in-flask-without-saving but it gave me an error

@app.route('/upload', methods=['POST'])
def fileUpload():

    if request.method == 'POST':
        f = request.files['file']
        fstring = f.read()
        csv_dicts = [{k: v for k, v in row.items()} for row in
                     csv.DictReader(fstring.splitlines(), skipinitialspace=True)]
    return "success"

         <form action = "/upload" method = "POST" enctype="multipart/form-data">
                <input type = "file" name = "file">
                <input type = submit>
            </form>

_csv.Error: iterator should return strings, not bytes (the file should be opened in text mode)

how can I manage this error?


Solution

Your error says your file is encoded, decode it.

fstring = f.read().decode("utf8")



Answered By - monkut
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing