Hướng dẫn extract data from multiple text files python - trích xuất dữ liệu từ nhiều tệp văn bản python

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.

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

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ó.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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 import0 import1 import2

    ____10import4 import0import6import7import8

    #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]
    
    6os0305 os2os3

    #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]
    
    6os5

    Output:

    //media.geeksforgeeks.org/wp-content/uploads/20210125102530/FreeOnlineScreenRecorderProject4.mp4

    Làm cách nào để đọc nhiều tệp văn bản trong Python?

    Làm cách nào để đọc nhiều tệp từ một thư mục trong Python ?..
    Nhập 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ục ..
    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ó ở hay không. Đị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 xử lý tệp ..

    Làm thế nào trích xuất dữ liệu cụ thể từ tệp văn bản trong Python?

    Cách trích xuất các phần cụ thể của tệp văn bản bằng Python..
    Hãy chắc chắn rằng bạn đang sử dụng Python 3 ..
    Đọc dữ liệu từ một tệp văn bản ..
    Sử dụng "với mở".
    Đọc các tệp văn bản theo từng dòng ..
    Lưu trữ dữ liệu văn bản trong một biến ..
    Tìm kiếm văn bản cho một chuỗi con ..
    Kết hợp các biểu thức thường xuyên ..
    Để tất cả chúng cùng nhau..

    Làm cách nào để nhập nhiều tệp vào Python?

    Chức năng Nhập khẩu [R]/ Nhập khẩu_File [Python] có thể được sử dụng để nhập nhiều tệp cục bộ bằng cách chỉ định thư mục và mẫu ...
    mẫu = "/a/. */iris_. ....
    mẫu = "/a/iris_. *": Nhập tất cả các tệp có mẫu/a/iris_.....
    mẫu = "/a/b/iris_. ....
    pattern="iris_..

    Làm cách nào để tạo nhiều tệp văn bản trong Python?

    Chỉ cần tạo nhiều tệp văn bản.Tạo cái này và sau đó, hoặc tạo một bó trong một vòng lặp, hoặc bất cứ điều gì bạn muốn ...
    File = Open [Hồi testfile.txt ,, W W].
    file.write [thế giới Hello Hello World].
    File.Write [Đây là tệp văn bản mới của chúng tôi].
    tập tin.....
    tập tin.....
    file.close[].

    Bài Viết Liên Quan

    Chủ Đề