Như đã đề cập trong các bình luận, một lỗi bạn đang mắc phải là bạn đang lặp qua một danh sách trống.
Đây là cách tôi sẽ làm điều đó, sử dụng một ví dụ về có 5 tệp Excel giống hệt nhau được thêm vào từng tệp khác.
[1] Nhập khẩu:
import os
import pandas as pd
[2] Tệp danh sách:
path = os.getcwd[]
files = os.listdir[path]
files
Output:
['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
[3] Chọn các tệp 'XLS':
files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
Output:
['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
[4] Khởi tạo DataFrame trống:
df = pd.DataFrame[]
[5] Vòng lặp qua danh sách các tệp để nối với DataFrame trống:
for f in files_xls:
data = pd.read_excel[f, 'Sheet1']
df = df.append[data]
[6] Tận hưởng DataFrame mới của bạn. :-]
df
Output:
Result Sample
0 a 1
1 b 2
2 c 3
3 d 4
4 e 5
5 f 6
6 g 7
7 h 8
8 i 9
9 j 10
0 a 1
1 b 2
2 c 3
3 d 4
4 e 5
5 f 6
6 g 7
7 h 8
8 i 9
9 j 10
0 a 1
1 b 2
2 c 3
3 d 4
4 e 5
5 f 6
6 g 7
7 h 8
8 i 9
9 j 10
0 a 1
1 b 2
2 c 3
3 d 4
4 e 5
5 f 6
6 g 7
7 h 8
8 i 9
9 j 10
0 a 1
1 b 2
2 c 3
3 d 4
4 e 5
5 f 6
6 g 7
7 h 8
8 i 9
9 j 10
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Approach:
- Nhập các gói python cần thiết như gấu trúc, địa cầu và hệ điều hành.
- Sử dụng Gói Glob Python để truy xuất các tệp/tên đường dẫn khớp với một mẫu được chỉ định, tức là ‘.xlsx
- Vòng lặp qua danh sách các tệp excel, đọc tệp đó bằng pandas.read_excel [].
- Chuyển đổi từng tệp excel thành DataFrame.
- Hiển thị vị trí, tên và nội dung của nó.
Dưới đây là việc thực hiện.
Python3
path = os.getcwd[]
files = os.listdir[path]
files
3 path = os.getcwd[]
files = os.listdir[path]
files
4path = os.getcwd[]
files = os.listdir[path]
files
3 path = os.getcwd[]
files = os.listdir[path]
files
6path = os.getcwd[]
files = os.listdir[path]
files
3 path = os.getcwd[]
files = os.listdir[path]
files
8path = os.getcwd[]
files = os.listdir[path]
files
9['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
0 ['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
1['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
2['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
0 ['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
4['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
5['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
6['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
7 ['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
8['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
9 files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
0files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
1files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
2['.DS_Store',
'.ipynb_checkpoints',
'.localized',
'Screen Shot 2013-12-28 at 7.15.45 PM.png',
'test1 2.xls',
'test1 3.xls',
'test1 4.xls',
'test1 5.xls',
'test1.xls',
'Untitled0.ipynb',
'Werewolf Modelling',
'~$Random Numbers.xlsx']
0 files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
4files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
1files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
6files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
7files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
8files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
9files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
1files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
6files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
7['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
3['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
4['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
5['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
6['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
7['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
8['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']
9files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
1files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
6files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
7df = pd.DataFrame[]
3df = pd.DataFrame[]
4files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
1df = pd.DataFrame[]
6files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
1files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
6df = pd.DataFrame[]
9Đầu ra:
Lưu ý: Chương trình đọc tất cả các tệp Excel trong thư mục mà chính chương trình có mặt. The program reads all Excel files in the folder in which the program itself is present.
Để đọc tất cả các tệp Excel trong một thư mục, hãy sử dụng mô -đun GLOB và phương thức read_excel [].
Hãy nói rằng sau đây là các tệp Excel của chúng tôi trong một thư mục -
Sales1.xlsx
Sales2.xlsx
Lúc đầu, đặt đường dẫn nơi đặt tất cả các tệp Excel. Nhận các tệp Excel và đọc chúng bằng cách sử dụng Glob -
path = "C:\Users\amit_\Desktop\" filenames = glob.glob[path + "\*.xlsx"] print['File names:', filenames]
Tiếp theo, sử dụng vòng lặp for để lặp lại và đọc tất cả các tệp Excels trong một thư mục cụ thể. Chúng tôi cũng đang sử dụng read_excel [] -
path = os.getcwd[]
files = os.listdir[path]
files
0Thí dụ
Sau đây là mã hoàn chỉnh -
path = os.getcwd[]
files = os.listdir[path]
files
1Đầu ra
Điều này sẽ tạo ra đầu ra sau -
path = os.getcwd[]
files = os.listdir[path]
files
2
Cập nhật vào ngày 27 tháng 9 năm 2021 11:28:01
- Câu hỏi và câu trả lời liên quan
- Làm thế nào để đọc dữ liệu từ tất cả các tệp trong một thư mục bằng Java?
- Python - Đọc tất cả các tệp CSV trong một thư mục trong gấu trúc?
- Python - Cách hợp nhất tất cả các tệp Excel trong thư mục
- Làm thế nào để hợp nhất tất cả các tệp CSV thành một DataFrame - Python Pandas?
- Làm thế nào để xóa tất cả các tệp trong một thư mục với Python?
- Làm thế nào để liệt kê tất cả các tệp trong một thư mục bằng Java?
- Làm thế nào để giải nén tất cả các tệp bị nén trong thư mục Linux?
- Làm thế nào để liệt kê tất cả các tệp [chỉ] từ một thư mục bằng Java?
- Làm thế nào để thực hiện thao tác grep trên tất cả các tệp trong một thư mục?
- Làm thế nào để hợp nhất nhiều tệp CSV thành một DataFrame Pandas?
- Làm thế nào để đọc tệp JSON vào DataFrame bằng thư viện Python Pandas?
- Chương trình Java để liệt kê tất cả các tệp trong một thư mục một cách đệ quy
- Chương trình Java để xóa tất cả các tệp trong thư mục một cách đệ quy [chỉ các tệp]
- PHP: tách tất cả các tệp trong một thư mục, sau đó xóa thư mục đó
- Làm cách nào để liệt kê tất cả các tệp của một thư mục trong Python?