我正在尝试使用以下简单代码从.CSV
文件中读取数据,将其拆分并保存为多个.txt
文件,然后使用以下代码进行加载以进行测试:
import pandas as pd
import numpy as np
data = 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.shape
count = 1
j = 25
for i in range(rows%25):
matxx = matx[i%25:j , :]
j += 25
np.savetxt(f' data{count}.txt ' , matxx, fmt='%d')
count +=1
nn = 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]
[path: C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Library\mingw-w64\bin;C:\ProgramData\Anaconda3\Library\usr\bin;C:\ProgramData\Anaconda3\Library\bin;C:\ProgramData\Anaconda3\Scripts;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\mingw64\bin;D:\cmake-3.11.3-win64-x64\cmake-3.11.3-win64-x64\bin;C:\opencv\build\install\x64\mingw\bin;C:\Program Files\nodejs\;C:\Program Files\MATLAB\R2018b\runtime\win64;C:\Program Files\MATLAB\R2018b\bin;C:\Program Files\Git\cmd;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\dotnet\;C:\Users\m\AppData\Local\Microsoft\WindowsApps;C:\Users\m\AppData\Roaming\npm;C:\Users\m\AppData\Local\Programs\Microsoft VS Code\bin]
有什么问题,我该如何解决?
答案 0 :(得分:3)
您应从保存的文件名中删除前导空格和尾随空格:
更改:
np.savetxt(f' data{count}.txt ' , matxx, fmt='%d')
收件人:
np.savetxt(f'data{count}.txt' , matxx, fmt='%d')