首页 > 解决方案 > 如何在python中导入订单列表中的所有excel文件?

问题描述

我已阅读 stackoverflow 建议的所有链接。从文件夹导入 excel 文件时出现问题。我files=os.listdir(path)在我的代码中使用了这个。但我不确定为什么列表不规则。

我通过定义路径从文件夹中导入了每个文件。350个文件全部导入,但列表格式不规则,与文件夹中的顺序不一样。

我写的代码是这样的:


path="C:/Nutakki/All_set/excel files/episode0/"
files=os.listdir(path)
print(files)
#m=['paketone4000.dump.xlsx','paketone8000.dump.xlsx','paketone1400000.dump.xlsx']
#fig_name=['i4000','i8000','i12000']
#
fig=plt.figure(figsize=(6,6)) ##to obtain figure and dimensions of graph

for index, i in enumerate(files):


    #ax=fig.add_subplot(111,projection='3d') ## to have a broad view of figure
    ax = fig.add_axes([0,0,1,1], projection='3d')

    #plot planes
    p = Rectangle((0,-0.7), 4.5,1.4, color="lightgrey", alpha=0.2) #plots the background frame
    ax.add_patch(p)

    art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")

    j=pd.read_excel(i)  ##to read the excel file format
    X=j['x'] ## to import the variable on to axes from data set
    Y=j['y']
    Z=j['z']
    #ax.scatter(X,Y,Z,c='g', marker='o') ## to specify the color and shape of point(marker) of the frame
#    print(j)
    a=j['x']##import centre of mass from excel file format
    b=j['y']
    c=j['z']

    q1=j['q1'],##attaining quaternons from excel file format. (comma(,) transformed series to tuple)
    q2=j['q2'],
    q3=j['q3'],
    q4=j['q4'],

    m2,n,o,p=np.array([q1,q2,q3,q4]) ## assigning quaternions to variables had converted tuple to float
    Rot_Mat=QtoR(m2,n,o,p)

    #cuboid initialising parameters
    center = [a[0], b[0], c[0]] ##centre of the body
    length = 0.3 ##defining length, breadth, height
    width = 0.4
    height = 0.1
    side = np.zeros((8,3))  ###This numpy vector will be used to store the position of the sides

    #rotate the axes and update
    for angle in range(0, 360):
        ax.view_init(90, angle)

    cuboid(center, (length, width, height)) #to execute the defined cuboid
    print('file_no:',i)

执行此操作后,我得到的列表如下:

['paketone0.dump.xlsx', 'paketone100000.dump.xlsx', 'paketone1000000.dump.xlsx', 'paketone1004000.dump.xlsx', 'paketone1008000.dump.xlsx', 'paketone1012000.dump.xlsx', 'paketone1016000.dump.xlsx', 'paketone1020000.dump.xlsx', 'paketone1024000.dump.xlsx', 'paketone1028000.dump.xlsx', 'paketone1032000.dump.xlsx', 'paketone1036000.dump.xlsx', 'paketone104000.dump.xlsx', 'paketone1040000.dump.xlsx', 'paketone1044000.dump.xlsx', 'paketone1048000.dump.xlsx', 'paketone1052000.dump.xlsx', 'paketone1056000.dump.xlsx', 'paketone1060000.dump.xlsx', 'paketone1064000.dump.xlsx', 'paketone1068000.dump.xlsx', 'paketone1072000.dump.xlsx', 'paketone1076000.dump.xlsx', 'paketone108000.dump.xlsx', 'paketone1080000.dump.xlsx', 'paketone1084000.dump.xlsx',...........]

预期结果应采用此列表格式

['paketone0.dump.xlsx','paketone4000.dump.xlsx','paketone8000.dump.xlsx','paketone12000.dump.xlsx','paketone16000.dump.xlsx','paketone20000.dump.xlsx','paketone24000.dump.xlsx','paketone28000.dump.xlsx','paketone32000.dump.xlsx','paketone36000.dump.xlsx','paketone40000.dump.xlsx','paketone44000.dump.xlsx','paketone48000.dump.xlsx',...........]

因为我的文件是文件夹中的这个命令 packtone(0 to 1400000).dump.xlsx。

标签: pythonexcellist

解决方案


您只需要按照自己的方式对最终列表进行排序

import re
from os import listdir
from os.path import isfile, join
mypath = "C:/Nutakki/All_set/excel files/episode0/"
m = [f for f in listdir(mypath) if isfile(join(mypath, f))]
pattern = re.compile(r"\d+")
m = sorted(m, key = lambda m :  int(pattern.findall(m)[0]) )

输出是

['paketone0.dump.xlsx',
 'paketone100000.dump.xlsx',
 'paketone104000.dump.xlsx',
 'paketone1000000.dump.xlsx',
 'paketone1004000.dump.xlsx',
 'paketone1008000.dump.xlsx',
 'paketone1012000.dump.xlsx',
 'paketone1016000.dump.xlsx',
 'paketone1020000.dump.xlsx',
 'paketone1024000.dump.xlsx',
 'paketone1028000.dump.xlsx',
 'paketone1032000.dump.xlsx',
 'paketone1036000.dump.xlsx',
 'paketone1040000.dump.xlsx']

推荐阅读