Today I encountered a read txt Problems with documents , For example, my target file is this
This data is python How should I read it
Method 1
with open(filename) as f:
lines = f.readlines()
label=[]
for line in lines:
#print(line)
label.append(int(line.replace('\n', '')))
label=np.array(label)
Notice the lines It's a long list , A return , Not line by line
Method 2
aa=np.loadtxt(filename,dtype=int)
print(aa)
Be careful to limit dtype, Otherwise, all that comes in is float