[英]Reading Two Numpy Arrays from csv file
So i want to read two 2D-arrays A and B from a csv file as a numpy-Array所以我想从 csv 文件中读取两个二维数组 A 和 B 作为 numpy-Array
i have the following csv file with ";"我有以下带有“;”的 csv 文件seperating two arrays:
分离两个 arrays:
A;B
1,1,2,2;3,3,4,4
6,7,3,7;3,5,3,5
1,8,5,3;6,1,7,5
The result should be something like this结果应该是这样的
A = [[1, 1, 2, 2], [6, 7, 3, 7], [1, 8, 5, 3]]
B = [[3, 3, 4, 4], [3, 7, 3, 7], [6, 1, 7, 5]]
now how i am supposed to do it, i tried alot with loadtxt and genfromtxt but couldn't do it现在我应该怎么做,我尝试了很多 loadtxt 和 genfromtxt 但做不到
Have you tried csv?你试过csv吗? I'm not posting the whole code, but something like this:
我没有发布整个代码,而是这样的:
import csv
with open('fileName.csv') as file:
csv_reader = csv.reader(file, delimiter=',')
for row in csv_reader:
#do sth
This should work, there may be a more numpy way, but this is how i made the array:这应该可行,可能有更多的 numpy 方式,但这就是我制作数组的方式:
import numpy as np
import pandas as pd
A=[]
B=[]
df1=pd.read_csv('numpy.csv', sep=";")
for x in range(len(df1.A)):
A.append(df1.A[x].split(','))
for x in range(len(df1.B)):
B.append(df1.B[x].split(','))
A=np.array(A).astype(np.int)
B=np.array(B).astype(np.int)
A
#array([[1, 1, 2, 2],
# [6, 7, 3, 7],
# [1, 8, 5, 3]])
B
Out[251]:
#array([[3, 3, 4, 4],
# [3, 5, 3, 5],
# [6, 1, 7, 5]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.