• Register
1 vote
105 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 24 views
24 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 9.9k points
1 vote
2 answers 119 views
119 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
0 votes
1 answer 12 views
12 views
Problem: How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list. Don't use file.readlines() in a for-loop, a file object itself is enough: lines = [line.rstrip('\n') for line in file]
asked Feb 19 Mashhoodch 9.9k points
0 votes
1 answer 10 views
10 views
Problem: How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list. Don't use file.readlines() in a for-loop, a file object itself is enough: lines = [line.rstrip('\n') for line in file]
asked Feb 13 Mashhoodch 9.9k points
0 votes
1 answer 12 views
0 votes
1 answer 18 views
18 views
Problem Hey, I struggle to create the list of dictionary in python. Can somebody help to find the way to create the list of dictionary in python. This will be a great honour for me.
asked Jan 14 chris jordan 2.4k points
0 votes
1 answer 24 views
24 views
Problem: I am having the list and I want to iterate over it to find the list of elements present in it or the sum of the element in it but don't know how to achieve this I am trying but getting a syntax error. Below is the code of this that I have tried ti iterate over ... .py", line 6, in <module> l=int(input("Enter the marks of subject:")) ValueError: invalid literal for int() with base 10: ''
asked Jun 12, 2020 Gavin 15.3k points
1 vote
1 answer 17 views
17 views
Problem: Hello guys, I have a big number of .csv file with full of data. I want to merge them all in a single file. It is a hectic job to copy all the data from each and compile into one single file. So, I am willing to combine multiple .csv files into one using ... me to do it. Thanks for your effort guys. Note: I named all the .csv files under a series. Let say they are named with data1-data100
asked Apr 6, 2020 Gavin 15.3k points
0 votes
1 answer 12 views
12 views
Problem: I have a terrible feeling that this will be a duplicate, I have already tried my best to find the answer. I have a dictionary and a list, and I want to create a list of dictionaries using a list to overwrite one of the key values, for example: d={"a":1,"b":10} c=[3,4,5 ... is a neater way with an iterator like arg=[d *with* d[a]=i for i in c] Where I'm not sure what to put instead of "c".
asked Dec 30, 2020 sasha 13.2k points