簡體   English   中英

如何從numpy文件的名稱中創建一個名稱列表

[英]how to create a numpy list of names from names of numpy files

我有一組numpy文件,它們位於一個文件夾中。 我需要創建一個numpy列表,在其中可以在每行中添加有關每個文件名的一些詳細信息:

示例:文件名:

AES_Trace=300001_key=000102030405060708090a0b0c0d0e0f_Plaintext=f9f19b259648feb20d842480745de16f_Ciphertext=a3140be40735f9f1865aa6b1b32b5667.npy

我列表中的每一行必須包括:

300001 000102030405060708090a0b0c0d0e0f f9f19b259648feb20d842480745de16f a3140be40735f9f1865aa6b1b32b5667 

我的意思是,我只需要輸入數字,然后輸入空格。

我使用這種方法,但是它給出了我文件的所有名稱。

import os
path_For_Numpy_Files = 'C:\\Users\\user\\My_Test_Traces\\1000_Traces_bin'
path_List_File='C:\\Users\\user\\My_Test_Traces\\NewTest.list_bin'
os.chdir(path_For_Numpy_Files)
list_files=os.listdir(os.getcwd())
with open(path_List_File, 'w') as fp:
    fp.write('\n'.join(sorted((list_files),key=os.path.getmtime)))

請問我可以改正它給我等待的結果嗎?

這似乎是基本思想。

from pathlib import Path
import re

p = Path('c:/scratch/sample')
for fileName in p.iterdir():
    print (fileName.name)
    print (' '.join(re.findall('=([0123456789abcdef]{2,})', fileName.name)))

該腳本的輸出為:

AES_Trace=300001_key=000102030405060708090a0b0c0d0e0f_Plaintext=f9f19b259648feb20d842480745de16f_Ciphertext=a3140be40735f9f1865aa6b1b32b5667.npy
300001 000102030405060708090a0b0c0d0e0f f9f19b259648feb20d842480745de16f a3140be40735f9f1865aa6b1b32b5667
AES_Trace=300002_key=000102030405060708090a0b0c0d0e0f_Plaintext=f9f19b259648feb20d842480745de16f_Ciphertext=a3140be40735f9f1865aa6b1b32b5667.npy
300002 000102030405060708090a0b0c0d0e0f f9f19b259648feb20d842480745de16f a3140be40735f9f1865aa6b1b32b5667
AES_Trace=300003_key=000102030405060708090a0b0c0d0e0f_Plaintext=f9f19b259648feb20d842480745de16f_Ciphertext=a3140be40735f9f1865aa6b1b32b5667.npy
300003 000102030405060708090a0b0c0d0e0f f9f19b259648feb20d842480745de16f a3140be40735f9f1865aa6b1b32b5667

您可以嘗試以下方法:

 def reformat_name(old):
        key0=  old.split('_key=')[1].split('_Plaintext=')[0]
        key1 = old.split('_Plaintext=')[1].split('_Ciphertext=')[0]
        key2 = old.split('_Ciphertext=')[1].split('.bin')[0]
        name = old.split('AES_Trace=')[1].split('_key=')[0]
        #new_filename =  '{:07d}'.format(int(name)) + '    ' + key0 + '    ' + key1 + '    '+ key2 + '    '
        new_filename = '{:07d}'.format(int(name)) + '    ' + key0 + '    ' + key1 + '    '+ key2 + '    '
        return new_filename

    import os
    path_For_Numpy_Files='C:\\Users\\user\\My_Test_Traces\\3_Traces_npy'
    path_List_File='C:\\Users\\user\\My_Test_Traces\\ttttttTest.list_npy'
    os.chdir(path_For_Numpy_Files)
    list_files_Without_Sort=os.listdir(os.getcwd())
    list_files_Sorted=sorted((list_files_Without_Sort),key=os.path.getmtime)
    with open(path_List_File, 'w') as fp:
        for file in list_files_Sorted:
            new_name = reformat_name(file)
            fp.write(new_name+ '\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM