0

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.

K-Doe
  • 509
  • 8
  • 29
  • So you only have two rows? – Dani Mesejo Jan 15 '19 at 14:32
  • At the Moment, yes! But there will be more in the future. – K-Doe Jan 15 '19 at 14:33
  • So you want to put all rows in lists? I think is better if you expand a little on what you are trying to achieve. Perhaps you could use pandas. – Dani Mesejo Jan 15 '19 at 14:34
  • At the Moment i have two rows with temperatures. High and Low. I Need those in two lists (at the moment). I never worked with Pandas. – K-Doe Jan 15 '19 at 14:35
  • Possible duplicate of [How do I read and write CSV files with Python?](https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) – buran Jan 15 '19 at 14:36
  • And ? What's your question exactly ? So far you explained what you wanted to do but didn't ask any question. – bruno desthuilliers Jan 15 '19 at 14:53
  • I updated my question. I hope ist okay like that. My question was answered. I think my question and the answere are clear to Show what i want to achive! – K-Doe Jan 15 '19 at 14:58

6 Answers6

1

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)
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

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)))
mad_
  • 8,121
  • 2
  • 25
  • 40
0

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.

0
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]
KuboMD
  • 684
  • 5
  • 16
0

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]
Daweo
  • 31,313
  • 3
  • 12
  • 25
0

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]