我正在尝试使用这个简单的代码从.CSV文件中读取数据,将其拆分并保存为多个.txt文件,然后使用以下代码加载以进行测试:import pandas as pdimport numpy as npdata = pd.read_csv("C:/Users/m/Desktop/python/IRB3MAIZ9936-a.csv") #, sep="\s")del data['DATE']matx = np.array(data.values, dtype= int)rows,cols = matx.shapecount = 1j = 25for i in range(rows%25): matxx = matx[i%25:j , :] j += 25 np.savetxt(f' data{count}.txt ' , matxx, fmt='%d') count +=1nn = np.loadtxt('data10.txt' , dtype = int)print()但我收到此错误消息:Traceback (most recent call last): File "C:\Users\m\Desktop\python\read_csv.py", line 19, in <module> nn = np.loadtxt('data10.txt' , dtype = int) File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 926, in loadtxt fh = np.lib._datasource.open(fname, 'rt', encoding=encoding) File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 262, in open return ds.open(path, mode, encoding=encoding, newline=newline) File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 618, in open raise IOError("%s not found." % path)OSError: data10.txt not found.[Finished in 2.0s with exit code 1][shell_cmd: python -u "C:\Users\m\Desktop\python\read_csv.py"][dir: C:\Users\m\Desktop\python]有什么问题,我该如何解决?
1 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
您应该从保存的文件名中删除前导空格和尾随空格:
改变:
np.savetxt(f' data{count}.txt ' , matxx, fmt='%d')
到:
np.savetxt(f'data{count}.txt' , matxx, fmt='%d')
添加回答
举报
0/150
提交
取消