• Register
1 vote
168 views

Problem:

Supose I have csv file named “liz.csv” which contains :

Serial,Name,Course,City
1,Raiyan,Python,London
2,Joel,Css,Kyoto
3,Samantha,Python,Paris
4,Porth,Java,Tokyo

How can I import this file into lists using python? 

Thank you

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes
 
Best answer

Answer:

Python has a built-in csv module, which provides a reader class to read the contents of a csv file.You can use that:

from csv import reader
 

with open('your file name.csv', 'r') as read_obj:
    
    csv_reader = reader(read_obj)
    
    list_of_rows = list(csv_reader)
    print(list_of_rows)

You can also use pandas to read csv files without headers:

import pandas as pd
 

df = pd.read_csv('your file name.csv', delimiter=',')
 

list_of_rows = [list(row) for row in df.values]
 

print(list_of_rows)

Hope you understand now . 

If not fell free to ask me any question.

11 6 4
34,950 points

Related questions

0 votes
1 answer 7 views
7 views
Problem: how to I Solve this: list index out of range error python HELP! I need help understanding this: list index out of range error python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 39 views
39 views
Problem: Please consider the following code: i = [1, 2, 3, 5, 8, 13] j = [] k = 0 for l in i: j[k] = l k += 1 print j The output (Python 2.6.6 on Win 7 32-bit) is: > Traceback (most recent call last): > j[k] = l IndexError: list assignment index out of range I guess it's something simple I don't understand. Can someone clear it up?
asked Jan 10 Mashhoodch 13k points
1 vote
2 answers 138 views
138 views
Problem: I am trying to adopt the python programming language. Learning it step by step and practicing daily. I’ve written simple code that contains a list and run an iteration on it but it shows: python indexerror: list assignment index out of range m = [1, 2, 3, 5, 8, 13] n = [] o = 0 for l in m: n[o] = l o += 1 print n Where did I wrong? Any Idea?
asked Mar 30, 2020 Gavin 15.3k points
1 vote
1 answer 10 views
10 views
Problem: I am unable to find out the solution, Please solve it : python read csv column into list
asked Mar 19 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 6 views
6 views
Problem: Can someone please help fix this uncaught error: python read csv file into array
asked Mar 19 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 5 views
5 views
Problem: Please help me. How can I solve it? python read csv into numpy array
asked Mar 19 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 28 views
28 views
Problem: ’ object does not support item assignment occurs when you attempt to alter or modify a character in a string using assignment operator. The string is an immutable object which can’t be changed. If you attempt to change the content of a string, the error TypeError: object does not support item assignment will be thrown.
asked Dec 2, 2020 Mashhoodch 13k points
0 votes
1 answer 4 views
4 views
Problem: I need someone to understand this problem and find out the solution. Creating a list of dictionaries in python
asked 10 hours ago Ifra 34.7k points
0 votes
1 answer 20 views