Tôi có một số tệp .txt và tôi cần trích xuất một số dữ liệu nhất định từ chúng. Các tập tin trông giống nhau, nhưng mỗi người trong số chúng lưu trữ dữ liệu khác nhau. Dưới đây là một ví dụ về tệp đó:
Start Date: 21/05/2016
Format: TIFF
Resolution: 300dpi
Source: X Company
...
Có nhiều thông tin hơn trong các tệp văn bản, nhưng tôi cần trích xuất ngày bắt đầu, định dạng và độ phân giải. Các tệp nằm trong cùng một thư mục cha ["E: \ Images"] nhưng mỗi tệp có thư mục riêng. Do đó, tôi cần một tập lệnh để đọc đệ quy các tệp này. Đây là kịch bản của tôi cho đến nay:
#importing a library
import os
#defining location of parent folder
BASE_DIRECTORY = 'E:\Images'
#scanning through subfolders
for dirpath, dirnames, filenames in os.walk[BASE_DIRECTORY]:
for filename in filenames:
#defining file type
txtfile=open[filename,"r"]
txtfile_full_path = os.path.join[dirpath, filename]
try:
for line in txtfile:
if line.startswidth['Start Date:']:
start_date = line.split[][-1]
elif line.startswidth['Format:']:
data_format = line.split[][-1]
elif line.startswidth['Resolution:']:
resolution = line.split[][-1]
print[
txtfile_full_path,
start_date,
data_format,
resolution]
Lý tưởng nhất là nó có thể tốt hơn nếu Python trích xuất nó cùng với một tên của tệp ECH và lưu nó trong một tệp văn bản. Bởi vì tôi không có nhiều kinh nghiệm về Python, tôi không biết làm thế nào để tiến xa hơn nữa.
Tôi cho rằng bạn đã biết cách nhập dữ liệu từ một tệp dữ liệu. Nếu không phải lo lắng, chúng tôi sẽ bắt đầu với nó.Một cách dễ dàng hơn để nhập, sắp xếp lại, hợp nhất và lập chỉ mục dữ liệu từ nhiều tệp văn bản cùng với việc xuất dữ liệu kết hợp.
Ảnh của Pixabay từ Pexels
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Prerequisite:
- Đọc
- Bàn luận
Xử lý tập tin
Approach:
- hệ điều hành
- Python là một ngôn ngữ mạnh mẽ, cực kỳ có khả năng ngay cả khi xử lý tập tin. Trong bài viết này, chúng tôi sẽ tìm hiểu cách đọc nhiều tệp văn bản từ một thư mục bằng Python.
- Nhập các mô -đun
- Thêm đường dẫn của thư mục
- Thay đổi thư mục
- Nhận danh sách một tệp từ một thư mụcFile Handling
Lặp lại thông qua danh sách tệp và kiểm tra xem phần mở rộng của tệp có ở định dạng .txt hay không.
- Nếu tệp văn bản tồn tại, hãy đọc tệp bằng cách sử dụng xử lýFile method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.
Các chức năng được sử dụng: os.chdir[path]
Parameters:
- Phương thức OS.Chdir [] trong Python được sử dụng để thay đổi thư mục làm việc hiện tại thành đường dẫn được chỉ định. Nó chỉ lấy một đối số duy nhất làm đường dẫn thư mục mới. A complete path of directory to be changed to new directory path.
Cú pháp: OS.Chdir [Path] Doesn’t return any value
- Đường dẫn: Một đường dẫn hoàn chỉnh của thư mục sẽ được thay đổi thành đường dẫn thư mục mới. method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then list of files and directories in the current working directory will be returned.
Cú pháp: Os.ListDir [Path] os.listdir[path]
Parameters:
- đường dẫn [tùy chọn]: đường dẫn của thư mục
Loại trả về: Phương thức này trả về danh sách tất cả các tệp và thư mục trong đường dẫn được chỉ định. Loại trả về của phương pháp này là danh sách. This method returns the list of all files and directories in the specified path. The return type of this method is list.
Dưới đây là việc thực hiện:
Program:
Python3
import
os
path
=
"Enter Folder Path"
os.chdir[path]
def
read_text_file[file_path]:
Các
#importing a library
import os
#defining location of parent folder
BASE_DIRECTORY = 'E:\Images'
#scanning through subfolders
for dirpath, dirnames, filenames in os.walk[BASE_DIRECTORY]:
for filename in filenames:
#defining file type
txtfile=open[filename,"r"]
txtfile_full_path = os.path.join[dirpath, filename]
try:
for line in txtfile:
if line.startswidth['Start Date:']:
start_date = line.split[][-1]
elif line.startswidth['Format:']:
data_format = line.split[][-1]
elif line.startswidth['Resolution:']:
resolution = line.split[][-1]
print[
txtfile_full_path,
start_date,
data_format,
resolution]
6#importing a library
import os
#defining location of parent folder
BASE_DIRECTORY = 'E:\Images'
#scanning through subfolders
for dirpath, dirnames, filenames in os.walk[BASE_DIRECTORY]:
for filename in filenames:
#defining file type
txtfile=open[filename,"r"]
txtfile_full_path = os.path.join[dirpath, filename]
try:
for line in txtfile:
if line.startswidth['Start Date:']:
start_date = line.split[][-1]
elif line.startswidth['Format:']:
data_format = line.split[][-1]
elif line.startswidth['Resolution:']:
resolution = line.split[][-1]
print[
txtfile_full_path,
start_date,
data_format,
resolution]
7#importing a library
import os
#defining location of parent folder
BASE_DIRECTORY = 'E:\Images'
#scanning through subfolders
for dirpath, dirnames, filenames in os.walk[BASE_DIRECTORY]:
for filename in filenames:
#defining file type
txtfile=open[filename,"r"]
txtfile_full_path = os.path.join[dirpath, filename]
try:
for line in txtfile:
if line.startswidth['Start Date:']:
start_date = line.split[][-1]
elif line.startswidth['Format:']:
data_format = line.split[][-1]
elif line.startswidth['Resolution:']:
resolution = line.split[][-1]
print[
txtfile_full_path,
start_date,
data_format,
resolution]
8#importing a library
import os
#defining location of parent folder
BASE_DIRECTORY = 'E:\Images'
#scanning through subfolders
for dirpath, dirnames, filenames in os.walk[BASE_DIRECTORY]:
for filename in filenames:
#defining file type
txtfile=open[filename,"r"]
txtfile_full_path = os.path.join[dirpath, filename]
try:
for line in txtfile:
if line.startswidth['Start Date:']:
start_date = line.split[][-1]
elif line.startswidth['Format:']:
data_format = line.split[][-1]
elif line.startswidth['Resolution:']:
resolution = line.split[][-1]
print[
txtfile_full_path,
start_date,
data_format,
resolution]
9 import
0 import
1 import
2____10import
4 import
0import
6import
7import
8
#importing a library
import os
#defining location of parent folder
BASE_DIRECTORY = 'E:\Images'
#scanning through subfolders
for dirpath, dirnames, filenames in os.walk[BASE_DIRECTORY]:
for filename in filenames:
#defining file type
txtfile=open[filename,"r"]
txtfile_full_path = os.path.join[dirpath, filename]
try:
for line in txtfile:
if line.startswidth['Start Date:']:
start_date = line.split[][-1]
elif line.startswidth['Format:']:
data_format = line.split[][-1]
elif line.startswidth['Resolution:']:
resolution = line.split[][-1]
print[
txtfile_full_path,
start_date,
data_format,
resolution]
6os
0305 os
2os
3#importing a library
import os
#defining location of parent folder
BASE_DIRECTORY = 'E:\Images'
#scanning through subfolders
for dirpath, dirnames, filenames in os.walk[BASE_DIRECTORY]:
for filename in filenames:
#defining file type
txtfile=open[filename,"r"]
txtfile_full_path = os.path.join[dirpath, filename]
try:
for line in txtfile:
if line.startswidth['Start Date:']:
start_date = line.split[][-1]
elif line.startswidth['Format:']:
data_format = line.split[][-1]
elif line.startswidth['Resolution:']:
resolution = line.split[][-1]
print[
txtfile_full_path,
start_date,
data_format,
resolution]
6os
5Output:
//media.geeksforgeeks.org/wp-content/uploads/20210125102530/FreeOnlineScreenRecorderProject4.mp4