我正在尝试从hi.txt文件中读取数据,hi.txt的内容显示在我上传的图片中。
我要做的是让数据看起来完全像下面的
X = [[0, 0], [0, 1], [1, 0], [1, 1]]
Y = [[0], [1], [1], [0]]
我的代码中包含的内容是
X=[]
Y=[]
和,hi.txt位于c:看起来像
#XOR
#X1 X2 Y
0 0 0
0 1 1
1 0 1
1 1 0
而且,我应该做些什么来通过阅读来创建这样的数据结构 txt数据..?
答案 0 :(得分:0)
我认为应该这样做。
[Route("~/")]
答案 1 :(得分:0)
使用numpy.loadtxt
:
In [30]: arr = np.loadtxt('Desktop/a.txt')
In [31]: X, Y = arr[:,:2], arr[:,2:]
In [32]: X
Out[32]:
array([[ 0., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 1.]])
In [33]: Y
Out[33]:
array([[ 0.],
[ 1.],
[ 1.],
[ 0.]])
答案 2 :(得分:0)
with open("hi.txt") as f:
X ,Y = [], []
for line in f:
if not line.startswith("#"):
x1, x2, y1 = line.strip().split()
X.append([x1, x2])
Y.append([y1])
答案 3 :(得分:0)
X = [[None for x in range(2)] for y in range(4)]
Y = [[None] for y in range(4)]
i=0
with open('hi.txt', 'r') as file:
for row in file:
if row[0]!="#":
a, b, c = row.split()
print ("\nReading row # %d from file: %s") %(i, row)
X[i]=[int(a),int(b)]
Y[i]=[int(c)]
i+=1
print "X[] is now:", X
print "Y[] is now:", Y
print ("\n\nFinal Output:")
print (X)
print (Y)