我覺得你與Python3其中使用Unicode作爲默認字符串類型的工作。字節串然後得到特殊的b
標記。
如果我生成使用Unicode而不是字節的數據,這個工程:
In [654]: data1 = np.zeros((3,),dtype=("U24,int,float"))
In [655]: data1['f0']='xxx' # more interesting string field
In [656]: with open('test.csv','w') as f:
writer=csv.writer(f,delimiter=',')
for row in data1:
writer.writerow(row)
In [658]: cat test.csv
xxx,0,0.0
xxx,0,0.0
xxx,0,0.0
np.savetxt
做同樣的事情:
In [668]: np.savetxt('test.csv',data1,fmt='%s',delimiter=',')
In [669]: cat test.csv
xxx,0,0.0
xxx,0,0.0
xxx,0,0.0
的問題是,我可以解決此,同時保持S24
字段?例如打開文件爲wb
?
我https://stackoverflow.com/a/27513196/901925 Trying to strip b' ' from my Numpy array
探討過這個問題,前面看起來像我的解決方案是要麼decode
字節字段,或者直接寫一個字節的文件。由於您的數組混合了字符串和數字字段,因此decode
解決方案更乏味。
data1 = data.astype('U24,i,f') # convert bytestring field to unicode
一個輔助功能,可用於decode
字節串上飛:
In [147]: fn = lambda row: [j.decode() if isinstance(j,bytes) else j for j in row]
In [148]: with open('test.csv','w') as f:
writer=csv.writer(f,delimiter=',')
for row in data:
writer.writerow(fn(row))
.....:
In [149]: cat test.csv
xxx,0,0.0
yyy,0,0.0
zzz,0,0.0
這看起來像[開放問題#4543](https://github.com/numpy/ numpy/issues/4543) – askewchan