This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: CSV import and export simplified
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: paullongnet, r.david.murray, rhettinger
Priority: normal Keywords: patch

Created on 2017-11-22 18:36 by paullongnet, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4450 closed paullongnet, 2017-11-24 18:46
Messages (6)
msg306746 - (view) Author: Paul Long (paullongnet) * Date: 2017-11-22 18:36
It would be helpful if the CSV module included standard functions that simplify the import of CSV files to a list and the export of CSV files to a list.  Here's an example of what they could be:

def csv2list(file_name): 
    list_name=[] 
    with open(file_name) as csvfile:
        readerfile = reader(csvfile)
        for row in readerfile:
            list_name.append(row)
    return list_name

def list2csv(list_name,file_name):
    with open(file_name, 'w', newline='') as csvfile:
        writerfile = csv.writer(csvfile)
        writerfile.writerows(list_name)
msg306747 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-11-22 18:44
Thanks for the suggestion, but I'm -0.5.  I don't think these are compelling enough to be worth adding to the module API.  The number of times I've done this kind of operation is far smaller than the number of times I've had other code in both the input and output loops.  *Far* smaller.
msg306748 - (view) Author: Paul Long (paullongnet) * Date: 2017-11-22 18:50
I should have added that there are thousands of 15 year old students completing GCSE Computer Science who would benefit massively from this.  All the exam boards in England require students to be able to read from and write to CSV files.  The complicated way of doing it in Python is making this quite inaccessible to most students and so although many programmers in general wouldn't need this very often, there are literally thousands of students who would benefit from this enhancement.
msg306749 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-11-22 19:32
Um.  If they aren't expected to understand how to write this code, why isn't it being provided to them as a canned function as part of the test environment?

To succeed in getting something like this added, I suspect you will need to show that it has more general utility, or there is something analogous that can be added that has more general utility.  I recommend starting a discussion on the python-ideas mailing list.

By the way, if I needed this in my code, I'd write:

  with open(file_name, newline="") as csvfile:
      my_list = list(csv.reader(csvfile))

  with open(file_name, 'w', newline="") as csvfile:
      csv.writer(csvfile).writerows(my_list)

Only if I needed it more than once would I bother wrapping it in a function.
msg306752 - (view) Author: Paul Long (paullongnet) * Date: 2017-11-22 19:40
Thanks for your advice.  The reason it's not provided as a canned function is because different schools can use different programming languages, although the vast majority choose to use Python.
msg306927 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-11-24 22:33
I'm also -1 on this feature request.  The open and closing of files is orthogonal to what you do with an open file.  Likewise, iterators are orthogonal to how they are consumed (for-loops, list(), set(), etc).

FWIW, csv.Reader object is an iterator that can be fed directly to list():

    result = list(csv.reader(fileobj)
History
Date User Action Args
2022-04-11 14:58:54adminsetgithub: 76297
2017-11-24 22:33:00rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg306927

resolution: rejected
stage: patch review -> resolved
2017-11-24 18:46:16paullongnetsetkeywords: + patch
stage: patch review
pull_requests: + pull_request4478
2017-11-22 19:40:44paullongnetsetmessages: + msg306752
2017-11-22 19:32:51r.david.murraysetmessages: + msg306749
2017-11-22 18:50:15paullongnetsetmessages: + msg306748
2017-11-22 18:44:30r.david.murraysetnosy: + r.david.murray
messages: + msg306747
2017-11-22 18:36:31paullongnetcreate