K-Doe
K-Doe

Reputation: 559

How to read CSV File and add each cell in a row to a list?

I have an CSV file which looks like this:

104 109 113 111 108 114
95  100 109 103 103 110

Two rows, and every number has its own cell.

How do i read the CSV file per row and add each cell of that row to a list?

The output should look like this:

List_1 = [104, 109, 113, 111, 108, 114]
List_2 = [95, 100, 109, 103, 103, 110]

Also i need the numbers in the list as integer not as a string.

Upvotes: 0

Views: 3961

Answers (6)

AymanAlkassem
AymanAlkassem

Reputation: 26

You could also solve it this way instead, using the csv.reader() module.

with open(filename) as f:
    reader = csv.reader(f)
    next(reader)

    list_ = []

    for row in reader:
        list_.append(row)

To access the first line (line_1 in the example)line_1 = list_[0]

Upvotes: 0

Daweo
Daweo

Reputation: 36590

If you would want to make later mathematical operations on your data, you might consider using numpy module for that, you can use numpy.loadtxt function (remember that csv files are in fact plain text files). Usage example: test.csv is file I created, it contain 2 rows each with 3 values

1 2 3
4 5 6

Then you can create numpy array following way:

import numpy as np
data = np.load('test.csv',int,delimiter=' ')

test.csv is name of file, int is information that you wants integers and delimiter=' ' means that values in file are space-separated. You can access values in numpy array by giving index of row and index of element

print(data[0][0]) #prints 1
print(data[1][2]) #prints 6

If you want to get list of values simply pass data[index_of_row] into list()

list1 = list(data[0])
list2 = list(data[1])
print(list1) #prints [1, 2, 3]
print(list2) #prints [4, 5, 6]

Upvotes: 0

RoadRunner
RoadRunner

Reputation: 26315

I would just store each row in a dictionary:

d = {}
with open('rows.csv') as f:
    for row, line in enumerate(f, start=1):
        d['Line_%d' % row] = list(map(int, line.split()))

print(d)
# {'Line_1': [104, 109, 113, 111, 108, 114], 'Line_2': [95, 100, 109, 103, 103, 110]}

Then you can access each row like this:

>>> d['Line_1']
[104, 109, 113, 111, 108, 114]
>>> d['Line_2']
[95, 100, 109, 103, 103, 110]

UPDATE:

As requested in the comments, if you file has a semicolon ; as a delimiter, you can use the csv library:

from csv import reader

d = {}
with open('rows.csv') as f:
    csv_reader = reader(f, delimiter=';')
    for i, line in enumerate(csv_reader, start=1):
        d['Line_%d' % i] = list(map(int, line))

print(d)

Upvotes: 1

KuboMD
KuboMD

Reputation: 684

my.csv:    
1 2 3 4 5
6 7 8 9 0

Python:

with open("my.csv") as f:
    lists = [ list(map(int, i.split())) for i in f.readlines() ]

print(lists[0])
print(lists[1])

Output:

[1, 2, 3, 4, 5]  
[6, 7, 8, 9, 0]

Upvotes: 0

Razmik Melikbekyan
Razmik Melikbekyan

Reputation: 758

You can use pandas read_csv function for reading CSV file. Afterwards you will have a DataFrame object. Using df.values.tolist() you will get the list of all rows as lists.

Upvotes: 0

mad_
mad_

Reputation: 8273

Just use map to transform list of string to list of ints and append in the output list

final=[]
with open('test.csv') as f:
    for row in f:
        final.append(list(map(int,row)))

Upvotes: 0

Related Questions