我试图将numpy数组写入txt文件,然后将该文件读回numpy数组

我试图将numpy数组写入txt文件,然后将该文件读回numpy数组,numpy,Numpy,我试图将numpy数组写入txt文件,然后将该文件读回numpy数组 import numpy as np from numpy import asarray from numpy import savetxt ##import csv ##import numpy a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) with open(r'test.txt', 'w') as f: f.write(" ".join(map(str, a)))

我试图将numpy数组写入txt文件,然后将该文件读回numpy数组

import numpy as np
from numpy import asarray
from numpy import savetxt
##import csv
##import numpy
a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

with open(r'test.txt', 'w') as f:
    f.write(" ".join(map(str, a)))

array = np.loadtxt(fname = "test.txt")
print(leafref)
这就是错误:

Traceback (most recent call last):
  File "C:/Users/MyPC/AppData/Local/Programs/Python/Python36/numpytocsv.py", line 12, in <module>
    array = np.loadtxt(fname = "test.txt")
  File "C:\Users\MyPC\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\lib\npyio.py", line 926, in loadtxt
    fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
  File "C:\Users\MyPC\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\lib\_datasource.py", line 260, in open
    return ds.open(path, mode, encoding=encoding, newline=newline)
  File "C:\Users\MyPC\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\lib\_datasource.py", line 608, in open
    found = self._findfile(path)
  File "C:\Users\MyPC\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\lib\_datasource.py", line 446, in _findfile
    if self.exists(name):
  File "C:\Users\MyPC\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\lib\_datasource.py", line 544, in exists
    from urllib.request import urlopen
  File "C:\Users\MyPC\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 84, in <module>
    import base64
  File "C:/Users/MyPC/AppData/Local/Programs/Python/Python36\base64.py", line 3, in <module>
    str = base64.b64encode(imageFile.read())
AttributeError: module 'base64' has no attribute 'b64encode'
回溯(最近一次呼叫最后一次):
文件“C:/Users/MyPC/AppData/Local/Programs/Python/Python36/numpytocsv.py”,第12行,在
array=np.loadtxt(fname=“test.txt”)
loadtxt中的文件“C:\Users\MyPC\AppData\Local\Programs\Python\36\lib\site packages\numpy\lib\npyio.py”,第926行
fh=np.lib.\u datasource.open(fname,'rt',encoding=encoding)
打开文件“C:\Users\MyPC\AppData\Local\Programs\Python\36\lib\site packages\numpy\lib\\u datasource.py”,第260行
返回ds.open(路径、模式、编码=编码、换行=换行)
文件“C:\Users\MyPC\AppData\Local\Programs\Python\36\lib\site packages\numpy\lib\\u datasource.py”,第608行,处于打开状态
已找到=自身。\u已找到文件(路径)
文件“C:\Users\MyPC\AppData\Local\Programs\Python\36\lib\site packages\numpy\lib\\u datasource.py”,第446行,在findfile中
如果self.exists(名称):
文件“C:\Users\MyPC\AppData\Local\Programs\Python\36\lib\site packages\numpy\lib\\u datasource.py”,第544行,存在
从urllib.request导入urlopen
文件“C:\Users\MyPC\AppData\Local\Programs\Python\Python36\lib\urllib\request.py”,第84行,在
导入base64
文件“C:/Users/MyPC/AppData/Local/Programs/Python/Python36\base64.py”,第3行,在
str=base64.b64encode(imageFile.read())
AttributeError:模块“base64”没有属性“b64encode”
备选方案:

In [87]: np.savetxt('test.txt', a, delimiter=',')                               
In [88]: cat test.txt                                                           
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
4.000000000000000000e+00
5.000000000000000000e+00
6.000000000000000000e+00
7.000000000000000000e+00
8.000000000000000000e+00
9.000000000000000000e+00
In [89]: np.savetxt('test.txt', a, delimiter=',', fmt='%6d')                    
In [90]: cat test.txt                                                           
     0
     1
     2
     3
     4
     5
     6
     7
     8
     9
In [91]: np.loadtxt('test.txt')                                                 
Out[91]: array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
保存二维阵列(一行):


尝试使用
加载文本,分隔符='
?如果保存的格式有效,则没有明显的理由调用bhase64
In [87]: np.savetxt('test.txt', a, delimiter=',')                               
In [88]: cat test.txt                                                           
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
4.000000000000000000e+00
5.000000000000000000e+00
6.000000000000000000e+00
7.000000000000000000e+00
8.000000000000000000e+00
9.000000000000000000e+00
In [89]: np.savetxt('test.txt', a, delimiter=',', fmt='%6d')                    
In [90]: cat test.txt                                                           
     0
     1
     2
     3
     4
     5
     6
     7
     8
     9
In [91]: np.loadtxt('test.txt')                                                 
Out[91]: array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
In [92]: np.savetxt('test.txt', a[None,:], delimiter=',', fmt='%6d')            

In [94]: cat test.txt                                                           
     0,     1,     2,     3,     4,     5,     6,     7,     8,     9
In [96]: np.loadtxt('test.txt',delimiter=',')                                   
Out[96]: array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])