Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to import data from CSV file that contains just one large row of data, into an array with separated values?

I currently have a CSV file that contains voltage values from an oscilloscope. The data is stored in a singular row made up of 5000 cells, like this:

[0, 0, 0, 2, 2, 2, 4, 2, 2, 2, 0, 0, 0...]

screenshot of part of excel file

When I try to import the data into an array, it creates an array of index 1, containing all 5000 values in that first array[0] index. So when I print array[0], it shows all 5000 values, and when I print array[1-4999], an error occurs. How can I have each value from the cell in its own spot in the array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my code:

import csv

array = []

with open("test2.csv", 'r') as f:
    cols = csv.reader(f, delimiter=",")
    for r in cols:
        array.append([r])

print(array[0])

>Solution :

import csv

array = []

with open("test2.csv", 'r') as f:
    cols = csv.reader(f, delimiter=",")
    # r is a single element
    for r in cols:
        # loop through each item in r
        for i in r:
            # append each element as list element
            array.append([i])

print(array)

Ouput:

[['0'], ['0'], ['0'], ['2'], ['4'], ['5']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading