OSError:找不到“.txt”

我正在尝试使用这个简单的代码从.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]

有什么问题,我该如何解决?


繁星淼淼
浏览 511回答 1
1回答

慕雪6442864

您应该从保存的文件名中删除前导空格和尾随空格:改变:np.savetxt(f'&nbsp;data{count}.txt&nbsp;'&nbsp;,&nbsp;matxx,&nbsp;fmt='%d')到:np.savetxt(f'data{count}.txt'&nbsp;,&nbsp;matxx,&nbsp;fmt='%d')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python