Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Python by (47.6k points)

I have a CSV file with about 2000 records.

Each record has a string and a category to it.

This is the first line, Line1 This is the second line, Line2 This is the third line, Line3

I need to read this file into a list that looks like this;

List = [('This is the first line', 'Line1'), ('This is the second line', 'Line2'), ('This is the third line', 'Line3')]

How can import this csv to the list I need using Python?

2 Answers

0 votes
by (106k points)

To import csv to list in python you can use the csv module:-

import csv 

with open('file.csv', 'rb') as f:

reader = csv.reader(f) 

your_list = list(reader) 

print(your_list)

0 votes
by (20.3k points)

Update for Python3:

import csv

with open('file.csv', 'r') as f:

  reader = csv.reader(f)

  your_list = list(reader)

print(your_list)

# [['This is the first line', 'Line1'],

#  ['This is the second line', 'Line2'],

#  ['This is the third line', 'Line3']]

Related questions

0 votes
1 answer
asked Sep 6, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
asked Jul 20, 2019 in Data Science by sourav (17.6k points)
0 votes
1 answer
asked Dec 6, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer

Browse Categories

...