2017-03-29 15 views
0

私はnumpyファイルのセットを1つのフォルダに持っています。numpyファイルの名前からnumpyの名前リストを作成する方法

例: ファイル名:私はすべての行に各file'nameに関するいくつかの詳細を置くことができます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))) 

私はそれを私に待ってくれた結果を教えてくれますか?

+0

'list_files'は、名前のリストです。そのリストを反復処理し、各文字列から必要な値を解析し、新しい文字列を作成する必要があります。これは普通のPythonのリストと文字列の仕事です。 numpyの問題ではありません( 'npy'拡張を除く)。 – hpaulj

答えて

0

あなたはこれを試すことがあります。

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') 
+0

ありがとう、それは動作します! – Guillaume

0

これは基本的な考えです。このスクリプトから

from pathlib import Path 
import re 

p = Path('c:/scratch/sample') 
for fileName in p.iterdir(): 
    print (fileName.name) 
    print (' '.join(re.findall('=([abcdef]{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 
関連する問題