Home Backend Development Python Tutorial Use python to convert npy format files to txt files

Use python to convert npy format files to txt files

Jul 23, 2020 pm 05:03 PM
python txt file Convert

Use python to convert npy format files to txt files

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

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--------------------------')
Copy after login

The following code converts the npy format file to txt and saves it to The same file name in the current directory

realizes the conversion of multiple files under the entire folder:

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')
Copy after login

The same code realizes reading a single npy file, reading and storing it as 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=',')
Copy after login

Modify the buffer size of pycharm's console:

If you use pycharm as the Python editor, the console's buf defaults to 1024. If there is too much output data, 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 idea.cycle.buffer value, 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

npy files save the weight of the network

Problem: Use gedit to open the npy file in the Ubuntu environment. It looks like this. You can’t see the content at all

Solution: Write the following code so that the decoded file content is output on the console

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

The file location is modified according to its own storage location

The output result 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],
   ...,
   ..........................................
Copy after login

Related learning recommendations: python video tutorial

The above is the detailed content of Use python to convert npy format files to txt files. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles