操作python如何實(shí)現(xiàn)npy格式文件轉(zhuǎn)換為txt文件

這篇文章將為大家詳細(xì)講解有關(guān)操作python如何實(shí)現(xiàn)npy格式文件轉(zhuǎn)換為txt文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)主營(yíng)山陽(yáng)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件開(kāi)發(fā)公司,山陽(yáng)h5微信小程序開(kāi)發(fā)搭建,山陽(yáng)網(wǎng)站營(yíng)銷(xiāo)推廣歡迎山陽(yáng)等地區(qū)企業(yè)咨詢(xún)

如下代碼會(huì)將npy的格式數(shù)據(jù)讀出,并且輸出來(lái)到控制臺(tái):

import numpy as np
 
##設(shè)置全部數(shù)據(jù),不輸出省略號(hào) 
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--------------------------')

如下代碼實(shí)現(xiàn)npy格式文件轉(zhuǎn)換為txt,并且保存到當(dāng)前目錄相同文件名

實(shí)現(xiàn)轉(zhuǎn)換整個(gè)文件夾下面多個(gè)文件:

import os
import numpy as np
path='./input_output' #一個(gè)文件夾下多個(gè)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' #一個(gè)文件夾下多個(gè)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')

同樣的代碼,實(shí)現(xiàn)讀取單個(gè)npy文件,讀取并且存儲(chǔ)為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的控制臺(tái)的buffer大小:

如果你是用pycharm作為Python的編輯器,那么控制臺(tái)的buf默認(rèn)為1024,如果輸出數(shù)據(jù)太多,需要修改buff大小才能讓

全部數(shù)據(jù)輸出,修改方法:

找到 pycharm 安裝目錄的 bin 目錄下 idea.properties 文件, 修改 idea.cycle.buffer 值,原來(lái)默認(rèn)為 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

補(bǔ)充知識(shí):讀取npy格式的文件

npy文件保存的是網(wǎng)絡(luò)的權(quán)重

問(wèn)題:Ubuntu環(huán)境下用gedit打開(kāi)npy文件,是這樣的,根本看不了內(nèi)容

操作python如何實(shí)現(xiàn)npy格式文件轉(zhuǎn)換為txt文件

解決方法:編寫(xiě)如下代碼,使解碼后的文件內(nèi)容輸出在控制臺(tái)

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

文件位置依據(jù)自己的存放位置進(jìn)行修改

運(yùn)行代碼輸出結(jié)果為

{'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],
   ...,
   ..........................................

關(guān)于操作python如何實(shí)現(xiàn)npy格式文件轉(zhuǎn)換為txt文件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

標(biāo)題名稱(chēng):操作python如何實(shí)現(xiàn)npy格式文件轉(zhuǎn)換為txt文件
網(wǎng)址分享:http://muchs.cn/article12/pihggc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、微信公眾號(hào)面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、域名注冊(cè)、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司