Home > Mobile >  How to import data from CSV file that contains just one large row of data, into an array with separa
How to import data from CSV file that contains just one large row of data, into an array with separa

Time:06-01

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?

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])

CodePudding user response:

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']]
  • Related