0

I'm new to Python so I'm sorry in advance if my question is basic.

I've got a CSV table that I imported using this code (I found this code on a post from StackOverflow):

import csv
with open('myCSV.csv', 'r') as csvfile:
    so = csv.reader(csvfile, delimiter=',', quotechar='"')
    so_data = []
    for row in so:
        so_data.append(row)

When I run so_data[1:][1], I get this: ['1.25;10.25;1.25;5.25']

I would rather like to get this: [1.25,10.25,1.25,5.25], so I can use this vector in an input for a Python function I'm using.

How can I get this? Thanks a lot for any suggestion.

I tried other ways such as importing CSV with myCSV=pd.read_csv(r'myCSV.csv'), using myCSV.iloc[[0]] and myCSV.iloc[0].values and myCSV.to_numpy()[0] but I dont get the vector format I need.

EDIT My data looks like that in the csv file:

a;b;c;d    
1.25;10.25;1.25;5.25    
11.25;2.25;1.25;5.25    
1.25;1.25;10.25;7.25

(NB: I tried to save the data in all the CSV formats allowed by Excel but I still could not get the vector format I need.)

eshirvana
  • 23,227
  • 3
  • 22
  • 38
statuser
  • 3
  • 3

4 Answers4

1

so your file delimiter is ; and not comma , so read the csv with proper delimiter and you will be good to go:

so = csv.reader(csvfile, delimiter=';', quotechar='"')

output:

>> print(so_data[1])
['11.25', '2.25', '1.25', '5.25']
eshirvana
  • 23,227
  • 3
  • 22
  • 38
0

When I run so_data[1:][1], I get this: ['1.25;10.25;1.25;5.25']

looks like your delimiter is a semicolon. try delimiter=';'.

even then each will still be a string. you will have to do something like:

for row in so:
    row = list(map(float, row))
    so_data.append(row)
richard
  • 144
  • 3
  • Thanks a lot for your comment (and also for editing my post)! I used pandas as proposed by @Marcos Santana, it worked. – statuser Jun 23 '23 at 16:06
0

When reading a file using csv.reader, the rows are treated as list of strings by default (See the oficial documentation [here][1]). You can try something like:

with open('../examples/data-1686519722934.csv','r') as csvfile:
so = csv.reader(csvfile, delimiter=',', quotechar='"')
so_data = []
for row in so:
    row_converted = [float(x) for x in ''.join(row).split(';')]
    so_data.append(my_list_conv)

This will iterate over the rows in our reader object (so) and convert each list of values into a string (''.join(row)), followed by splitting on the ;. This generates a list of strings that are then converted into floats using float(x).

It seems your delimiter is not ',', but ';'. You should fix that first.

Another solution is to just use pandas to open the csv file.

import pandas as pd
my_csv = pd.read_csv(, delimiter=';')
first_row = my_csv.values[0]
print(first_row)
Out: array([1.25, 10.25, 1.25, 5.25])

[1]: https://docs.python.org/3/library/csv.html#:~:text=csv.reader,transformed%20into%20floats).

0

I think the iterrows function is what you're looking for, as described in this answer. I would then access each element in the series of a given row, and insert them into a new list (vector).