I'm asking you a question after revising it again. Extract specific files from list

Asked 2 years ago, Updated 2 years ago, 47 views

Hi, how are you?

For example, the file name has the following specific patterns:

3G1-1EW_E-W_ACC
3G1-1NS_N-S_ACC
3G1-1UD_U-D_ACC
3G1-2EW_E-W_ACC
3G1-2NS_N-S_ACC
3G1-2UD_U-D_ACC

When referred to as abc-def_g-h_ijk

Can a, b, c, and d find the same file name and extract it??

For example, there are 6 files in the list as shown above

3G1-1EW_E-W_ACC
3G1-1NS_N-S_ACC
3G1-1UD_U-D_ACC

Find and extract only 3 files that meet the conditions as follows If you have a file

 file1=pd.read_csv('3G1-1EW_E-W_ACC')
 file2=pd.read_csv('3G1-1NS_N-S_ACC')
 file3=pd.read_csv('3G1-1UD_U-D_ACC')

Specify as pd.concat and merge the 3G1-1.csv files with pd.concat

Go back to

 file1=pd.read_csv('3G1-2EW_E-W_ACC')
 file2=pd.read_csv('3G1-2NS_N-S_ACC')
 file3=pd.read_csv('3G1-2UD_U-D_ACC')

Similarly, you would like to merge the 3G1-2.csv files.

ii = [i for i in os.listdir(targerdir) if '3G1-1' in i]

I did the fairy degree, but I had to change the conditional sentence 3G1-1 every time, Do you have any other routines?

python list

2022-09-20 14:55

1 Answers

Please keep that in mind.

L = [
    '3G1-1EW_E-W_ACC',
    '3G1-1NS_N-S_ACC',
    '3G1-1UD_U-D_ACC',
    '3G1-2EW_E-W_ACC',
    '3G1-2NS_N-S_ACC',
    '3G1-2UD_U-D_ACC'
]
import itertools as it

g = it.groupby(L, lambda name:name[:5])
for k, v in g:
    print(k, list(v))
3G1-1 ['3G1-1EW_E-W_ACC', '3G1-1NS_N-S_ACC', '3G1-1UD_U-D_ACC']
3G1-2 ['3G1-2EW_E-W_ACC', '3G1-2NS_N-S_ACC', '3G1-2UD_U-D_ACC']


2022-09-20 14:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.