如何将仅包含一大行数据的 CSV 文件中的数据导入具有分隔值的数组?
How to import data from CSV file that contains just one large row of data, into an array with separated values?
我目前有一个 CSV 文件,其中包含来自示波器的电压值。数据存储在由 5000 个单元格组成的单行中,如下所示:
[0, 0, 0, 2, 2, 2, 4, 2, 2, 2, 0, 0, 0...]
screenshot of part of excel file
当我尝试将数据导入数组时,它创建了一个索引为 1 的数组,其中包含第一个数组 [0] 索引中的所有 5000 个值。所以当我打印 array[0] 时,它显示了所有 5000 个值,而当我打印 array[1-4999] 时,出现错误。我怎样才能让单元格中的每个值都位于数组中它自己的位置?
这是我的代码:
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])
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)
输出:
[['0'], ['0'], ['0'], ['2'], ['4'], ['5']]
我目前有一个 CSV 文件,其中包含来自示波器的电压值。数据存储在由 5000 个单元格组成的单行中,如下所示:
[0, 0, 0, 2, 2, 2, 4, 2, 2, 2, 0, 0, 0...]
screenshot of part of excel file
当我尝试将数据导入数组时,它创建了一个索引为 1 的数组,其中包含第一个数组 [0] 索引中的所有 5000 个值。所以当我打印 array[0] 时,它显示了所有 5000 个值,而当我打印 array[1-4999] 时,出现错误。我怎样才能让单元格中的每个值都位于数组中它自己的位置?
这是我的代码:
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])
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)
输出:
[['0'], ['0'], ['0'], ['2'], ['4'], ['5']]