[Tutor] CSV Module , Kindly treat my last question with same subject as cancelled

Cameron Simpson cs at cskk.id.au
Mon Sep 28 01:23:57 EDT 2020


On 26Sep2020 19:49, Manprit Singh <manpritsinghece at gmail.com> wrote:
>Now coming to the main question . if i want these four rows in the form 
>of
>nested list as shown below :
>
>[['1', 'Andy', 'Science'],
> ['2', 'Robin', 'Arts'],
> ['3', 'Serena', 'Arts'],
> ['4', 'Villiams', 'Commerce']]
>
>The below written code is an efficient implementation for the output shown
>above ?
>
>l = []
>with open('some.csv', newline='') as f:
>    reader = csv.reader(f)                      # Reader object
>    for row in reader:
>        l.append(row)

Well, the list() constructor accepts any iterable, so:

    with open('some.csv', newline='') as f:
        l = list(csv.reader(f))

since csv.reader(f) returns an iterable of CSV rows.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list