Python realizes the operation of converting npy format files to txt files

The following code will read the format data of npy , and output to the console:

import numpy as np ##Set all data without outputting ellipsis import sysnp.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- -------------------------')


The following code realizes the conversion of npy format files to txt and saves them to the same file name in the current directory

Realize the conversion of multiple files under the entire folder:

import osimport numpy as nppath='./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 osimport numpy as nppath='./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')


The same code realizes reading a single npy file, reading and storing it as txt:

import numpy as npinput_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=',')


Modify the buffer size of the pycharm console:

If you are using pycharm as a Python editor, the buf of the console is 1024 by default. If the output data is too much, you need to modify the buff size to allow

All data output, modification method:

Find the idea.properties file in the bin directory of the pycharm installation directory, modify the value of idea.cycle.buffer, the original default is 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

Supplementary knowledge: read files in npy format

The npy file saves the weight of the network

Problem: Open the npy file with gedit in the Ubuntu environment, it is like this, I can't see the content at all


v2-6cf7dd23788441d11a68c428f66fb0c4_720w.jpeg


Solution: Write the following code to output the decoded file content in the console

import numpy as npcontext = np.load('E:/KittiSeg_pretrained0/vgg16.npy',encoding="latin1")print(context)


The file location is modified according to your own storage location

The output of running the code is

{'conv1_2': [array([[[[ 1.66219279e-01, 1.42701820e-01, -4.02113283e-03, ..., 6.18828237e-02, -1.74057148e-02, -3.00644431e-02], [ 9.46945231e-03, 3.87477316e-03, 5.08365929e-02, ..., -2.77981739e-02, 1.71373668e-03, 6.82722731e-03], [ 6.32681847e-02, 2.12877709e-02, -1.63465310e-02, ..., 8.80054955e-04, 6.68104272e-03, -1.41139806e-03], ..., [ 3.47490981e-03, 8.47019628e-02, -4.07223180e-02, ..., -1.13523193e-02, -7.48998486e-03, 3.19077494e-03], [ 5.97234145e-02, 4.97663505e-02, -3.23118735e-03, ..., 1.43114366e-02, 3.03175431e-02, -4.23925705e-02], [ 1.33459672e-01, 4.95484173e-02, -1.78808011e-02, ..., 2.25385167e-02, 3.02020740e-02, -2.17075031e-02]],  [[ 2.12007999e-01, 2.10127644e-02, -1.47626130e-02, ..., 2.29580477e-02, 1.23102348e-02, -3.08422819e-02], [-2.62175221e-03, 7.42094172e-03, 6.74030930e-02, ..., -3.06594316e-02, 1.80578313e-03, 4.27369215e-03], (2.27197763e-02, -1.07841045e-02, -1.31095545e-02, ..., -1.15751950e-02, 4.18359675e-02, -1.92268589e-03), ..., (-2.70304317e-03, 7.41161704e-02, -3.32262330e-02, ..., -1.10277236e-02, 1.39831286e-02, 5.34419343e-03), (-3.20506282e-02, -2.40584910e-02, -4.52397857e-03, ..., -6.04042644e-03, 2.01962605e-01, -5.04491515e-02], (1.68114193e-02, -2.33167298e-02, -1.40886130e- 02, ..., -7.79278344e-03, 1.28428593e-01, -2.58184522e-02]], [(-5.91698708e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e -04, -4.84175496e-02, 1.63675251e-03], (-3.93767562e-03, 9.07397643e-03, 5.36517277e-02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e -03], (-3.07008922e-02, -1.09781921e-02, -3.69096454e-03, ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03), ...,. .........................................27369215e-03], [2.27197763e-02, -1.07841045e-02, -1.31095545e-02, ..., -1.15751950e-02, 4.18359675e-02, -1.92268589e-03], ..., ( -2.70304317e-03, 7.41161704e-02, -3.32262330e-02, ..., -1.10277236e-02, 1.39831286e-02, 5.34419343e-03], (-3.20506282e-02, -2.40584910e-02 , -4.52397857e-03, ..., -6.04042644e-03, 2.01962605e-01, -5.04491515e-02], (1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ... , -7.79278344e-03, 1.28428593e-01, -2.58184522e-02]], [(-5.91698708e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496 e-02, 1.63675251e-03], (-3.93767562e-03, 9.07397643e-03, 5.36517277e-02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03), ( -3.07008922e-02, -1.09781921e-02, -3.69096454e-03, ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03), ..., ...... ....................................27369215e-03], [2.27197763e-02, -1.07841045e-02, -1.31095545e-02, ..., -1.15751950e-02, 4.18359675e-02, -1.92268589e-03], ..., ( -2.70304317e-03, 7.41161704e-02, -3.32262330e-02, ..., -1.10277236e-02, 1.39831286e-02, 5.34419343e-03], (-3.20506282e-02, -2.40584910e-02 , -4.52397857e-03, ..., -6.04042644e-03, 2.01962605e-01, -5.04491515e-02], (1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ... , -7.79278344e-03, 1.28428593e-01, -2.58184522e-02]], [(-5.91698708e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496 e-02, 1.63675251e-03], (-3.93767562e-03, 9.07397643e-03, 5.36517277e-02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03), ( -3.07008922e-02, -1.09781921e-02, -3.69096454e-03, ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03), ..., ...... ....................................31095545e-02, ..., -1.15751950e-02, 4.18359675e-02, -1.92268589e-03], ..., (-2.70304317e-03, 7.41161704e-02, -3.32262330e-02, ... ., -1.10277236e-02, 1.39831286e-02, 5.34419343e-03], (-3.20506282e-02, -2.40584910e-02, -4.52397857e-03, ..., -6.04042644e-03, 2.01962605e -01, -5.04491515e-02], (1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ..., -7.79278344e-03, 1.28428593e-01, -2.58184522e-02]) , [(-5.91698708e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496e-02, 1.63675251e-03], (-3.93767562e-03, 9.07397643e) -03, 5.36517277e-02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03], (-3.07008922e-02, -1.09781921e-02, -3.69096454e-03,. ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03], ..., ......................... .................31095545e-02, ..., -1.15751950e-02, 4.18359675e-02, -1.92268589e-03], ..., (-2.70304317e-03, 7.41161704e-02, -3.32262330e-02, ... ., -1.10277236e-02, 1.39831286e-02, 5.34419343e-03], (-3.20506282e-02, -2.40584910e-02, -4.52397857e-03, ..., -6.04042644e-03, 2.01962605e -01, -5.04491515e-02], (1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ..., -7.79278344e-03, 1.28428593e-01, -2.58184522e-02]) , [(-5.91698708e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496e-02, 1.63675251e-03], (-3.93767562e-03, 9.07397643e) -03, 5.36517277e-02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03], (-3.07008922e-02, -1.09781921e-02, -3.69096454e-03,. ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03], ..., ......................... .................7.41161704e-02, -3.32262330e-02, ..., -1.10277236e-02, 1.39831286e-02, 5.34419343e-03], (-3.20506282e-02, -2.40584910e-02, -4.52397857e-03 , ..., -6.04042644e-03, 2.01962605e-01, -5.04491515e-02], [1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ..., -7.79278344e-03 , 1.28428593e-01, -2.58184522e-02]], ((-5.91698708e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496e-02, 1.63675251e -03], (-3.93767562e-03, 9.07397643e-03, 5.36517277e-02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03), (-3.07008922e-02, -1.09781921e-02, -3.69096454e-03, ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03], ..., ............ ..............................7.41161704e-02, -3.32262330e-02, ..., -1.10277236e-02, 1.39831286e-02, 5.34419343e-03], (-3.20506282e-02, -2.40584910e-02, -4.52397857e-03 , ..., -6.04042644e-03, 2.01962605e-01, -5.04491515e-02], [1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ..., -7.79278344e-03 , 1.28428593e-01, -2.58184522e-02]], ((-5.91698708e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496e-02, 1.63675251e -03], (-3.93767562e-03, 9.07397643e-03, 5.36517277e-02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03), (-3.07008922e-02, -1.09781921e-02, -3.69096454e-03, ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03], ..., ............ ..............................04491515e-02], [1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ..., -7.79278344e-03, 1.28428593e-01, -2.58184522e-02]], [[-5.91698708 e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496e-02, 1.63675251e-03], (-3.93767562e-03, 9.07397643e-03, 5.36517277e -02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03], (-3.07008922e-02, -1.09781921e-02, -3.69096454e-03, ..., -1.19221993 e-02, -1.39777903e-02, 8.52933805e-03], ..., .............................. ............04491515e-02], [1.68114193e-02, -2.33167298e-02, -1.40886130e-02, ..., -7.79278344e-03, 1.28428593e-01, -2.58184522e-02]], [[-5.91698708 e-03, -2.26223674e-02, 4.88128467e-03, ..., 4.13784146e-04, -4.84175496e-02, 1.63675251e-03], (-3.93767562e-03, 9.07397643e-03, 5.36517277e -02, ..., -2.56106984e-02, -4.17886395e-03, 2.47476017e-03], (-3.07008922e-02, -1.09781921e-02, -3.69096454e-03, ..., -1.19221993 e-02, -1.39777903e-02, 8.52933805e-03], ..., .................................. ............09781921e-02, -3.69096454e-03, ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03], ..., .............. ............................09781921e-02, -3.69096454e-03, ..., -1.19221993e-02, -1.39777903e-02, 8.52933805e-03], ..., .............. ............................


The above python implementation of converting npy format files to txt files is all the content shared by the editor. I hope I can give you a reference and hope you can support it.


Guess you like

Origin blog.51cto.com/14825302/2540275