我想对包含如下数据的excel文件进行排序:
1.jpg
CC Library - Screen for proxy.png
aAd.ai
aaa.PSD
automation_35.jpg
automation_36.jpg
automation_37.jpg
automation_371.jpg
sad.psd
sde-Recovered.ai
sde-Recovered.psd
sde-Recovered1.psd
testfile.ai
testfile.psd
vpTestID (5)as.indd
wulogo15117.jpg
如果我正在使用pandas,我使用df.sort_values
或sort_index
得到的结果是
import pandas as pd
df=pd.read_excel('data.xlsx',sheet_name='Sheet1')
result = df.sort_index()
print(result)
结果是:
1.jpg
CC Library - Screen for proxy.png
aAd.ai
aaa.PSD
automation_35.jpg
automation_36.jpg
automation_37.jpg
automation_371.jpg
sad.psd
sde-Recovered.ai
sde-Recovered.psd
sde-Recovered1.psd
testfile.ai
testfile.psd
vpTestID (5)as.indd
wulogo15117.jpg
wulogo15117.jpg
但是,如果我使用win32api
import win32com.client
excel = win32com.client.Dispatch("Excel.Application")
wb = excel.Workbooks.Open('data.xlsx')
ws = wb.Worksheets('Sheet1')
ws.Range('A1:A100').Sort(Key1=ws.Range('A1'), Order1=1, Orientation=1)
wb.Save()
excel.Application.Quit()
我正在获取数据
1.jpg
aaa.PSD
aAd.ai
automation_35.jpg
automation_36.jpg
automation_37.jpg
automation_371.jpg
CC Library - Screen for proxy.png
sad.psd
sde-Recovered.ai
sde-Recovered.psd
sde-Recovered1.psd
testfile.ai
testfile.psd
vpTestID (5)as.indd
wulogo15117.jpg
我想要数据(win32api给出的方式)格式进行比较。这对我在Windows机器上有效,但在Mac机器上会失败,因为它没有win32api。
有人可以帮我在Mac上以相同的格式获取数据。
您需要使用lower对值进行排序(将字符串转换为小写):
import pandas as pd
lst = ['1.jpg',
'aAd.ai',
'CC Library - Screen for proxy.png',
'aaa.PSD',
'automation_35.jpg',
'automation_36.jpg',
'automation_37.jpg',
'automation_371.jpg',
'sad.psd',
'sde-Recovered.ai',
'sde-Recovered.psd',
'sde-Recovered1.psd',
'testfile.ai',
'testfile.psd',
'vpTestID (5)as.indd',
'wulogo15117.jpg']
series = pd.Series(data=sorted(lst, key=lambda e: e.lower()))
print(series)
产量
0 1.jpg
1 aaa.PSD
2 aAd.ai
3 automation_35.jpg
4 automation_36.jpg
5 automation_37.jpg
6 automation_371.jpg
7 CC Library - Screen for proxy.png
8 sad.psd
9 sde-Recovered.ai
10 sde-Recovered.psd
11 sde-Recovered1.psd
12 testfile.ai
13 testfile.psd
14 vpTestID (5)as.indd
15 wulogo15117.jpg
dtype: object