python实现npy格式文件转换为txt文件

 


如下代码会将npy的格式数据读出,并且输出来到控制台:

import numpy as np

##设置全部数据,不输出省略号 
import sys
np.set_printoptions(threshold=sys.maxsize)

boxes=np.load('./input_output/boxes.npy')
print(boxes)
np.savetxt('./input_output/boxes.txt',boxes,fmt='%s',newline='\n')
print('---------------------boxes--------------------------')

 如下代码实现npy格式文件转换为txt,并且保存到当前目录相同文件名 

实现转换整个文件夹下面多个文件:

import os
import numpy as np
path='./input_output' #一个文件夹下多个npy文件,
txtpath='./input_output'
namelist=[x for x in os.listdir(path)]
for i in range( len(namelist) ):
	datapath=os.path.join(path,namelist[i]) #specific address
	print(namelist[i])
	data = np.load(datapath).reshape([-1, 2])  # (39, 2)
	np.savetxt('%s/%s.txt'%(txtpath,namelist[i]),data)
print ('over')
import os
import numpy as np
path='./input_output' #一个文件夹下多个npy文件
txtpath='./input_output'
namelist=[x for x in os.listdir(path)]
for i in range( len(namelist) ):
	datapath=os.path.join(path,namelist[i]) #specific address
	print(namelist[i])
	#data = np.load(datapath).reshape([-1, 2])  # (39, 2)
	input_data = np.load(datapath) # (39, 2)
	data = input_data.reshape(1, -1)
	np.savetxt('%s/%s.txt'%(txtpath,namelist[i]),data)
print ('over')

 

同样的代码,实现读取单个npy文件,读取并且存储为txt :

import numpy as np
input_data = np.load(r"C:\test.npy")
print(input_data.shape)
data = input_data.reshape(1,-1)
print(data.shape)
print(data)
np.savetxt(r"C:\test.txt",data,delimiter=',')

修改pycharm的控制台的buffer大小:

如果你是用pycharm作为Python的编辑器,那么控制台的buf默认为1024,如果输出数据太多,需要修改buff大小才能让全部数据输出,修改方法:

找到 pycharm 安装目录的 bin 目录下 idea.properties 文件, 修改 idea.cycle.buffer 值,原来默认为 1024 

#---------------------------------------------------------------------
# This option controls console cyclic buffer: keeps the console output size not higher than the specified buffer size (Kb).
# Older lines are deleted. In order to disable cycle buffer use idea.cycle.buffer.size=disabled
#---------------------------------------------------------------------
idea.cycle.buffer.size=102400 

参考:https://blog.csdn.net/weixin_40490880/article/details/84066944 

你可能感兴趣的:(AI)