Hướng dẫn how do i extract data from a python file? - làm cách nào để trích xuất dữ liệu từ tệp python?

Để đọc một dòng tệp từng dòng, chỉ cần lặp qua đối tượng tệp đang mở trong vòng lặp

words = line.split[]
4:

for line in open[filename]:
    # do something with line

Để chia một dòng theo khoảng trắng vào danh sách các từ riêng biệt, hãy sử dụng

words = line.split[]
5:

words = line.split[]

Để đếm số lượng mục trong danh sách Python, hãy sử dụng

words = line.split[]
6:

count = len[words]

Để chọn hai mục đầu tiên từ danh sách Python, hãy sử dụng cắt lát:

firsttwo = words[:2]

Tôi sẽ để xây dựng chương trình hoàn chỉnh cho bạn, nhưng bạn sẽ không cần nhiều hơn những điều trên, cộng với một câu lệnh

words = line.split[]
7 để xem bạn đã có hai từ của mình.

Ba byte phụ mà bạn thấy khi bắt đầu tệp của bạn là BOM UTF-8 [Dấu thứ tự byte]; Nó đánh dấu tệp của bạn là UTF-8 được mã hóa, nhưng nó là dự phòng và chỉ thực sự được sử dụng trên Windows.

Bạn có thể loại bỏ nó bằng:

import codecs
if line.startswith[codecs.BOM_UTF8]:
    line = line[3:]

Bạn có thể muốn giải mã chuỗi của mình thành Unicode bằng cách sử dụng mã hóa đó:

line = line.decode['utf-8']

Bạn cũng có thể mở tệp bằng

words = line.split[]
8:

file = codecs.open[filename, encoding='utf-8']

Lưu ý rằng

words = line.split[]
8 sẽ không tước BOM cho bạn; Cách dễ nhất để làm điều đó là sử dụng
count = len[words]
0:

import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]

Đôi khi làm việc với một số bộ dữ liệu phải chủ yếu làm việc với các tệp .csv [giá trị phân tách bằng dấu phẩy]. Chúng thực sự là một điểm khởi đầu tuyệt vời trong việc áp dụng các kỹ thuật và thuật toán khoa học dữ liệu. Nhưng nhiều người trong chúng ta sẽ tham gia vào các công ty khoa học dữ liệu hoặc tham gia các dự án trong thế giới thực trong khoa học dữ liệu sớm hay muộn. Thật không may trong các dự án trong thế giới thực, dữ liệu đã giành được cho chúng tôi trong một tệp .csv gọn gàng. Ở đó chúng tôi phải trích xuất dữ liệu từ các nguồn khác nhau như hình ảnh, tệp PDF, tệp tài liệu, tệp hình ảnh, v.v. Trong bài viết này, chúng tôi sẽ thấy sự khởi đầu hoàn hảo để giải quyết các tình huống đó..csv[Comma Separated Value] files only. They are really a great starting point in applying Data Science techniques and algorithms. But many of us will land up in Data Science firms or take up real-world projects in Data Science sooner or later. Unfortunately in real-world projects, the data won’t be available to us in a neat .csv file. There we have to extract data from different sources like images, pdf files, doc files, image files, etc. In this article, we will see the perfect start to tackle those situations.

Dưới đây chúng ta sẽ thấy cách trích xuất thông tin liên quan từ nhiều nguồn như vậy.

1. Nhiều tệp Excel

Lưu ý rằng nếu tệp excel có một bảng duy nhất thì cùng một phương thức để đọc tệp CSV [pd.read_csv [‘file.xlsx,]] có thể hoạt động. Nhưng nó đã giành chiến thắng trong trường hợp nhiều tệp trang tính như trong hình ảnh dưới đây có 3 tờ [Sheet1, Sheet2, Sheet3]. Trong trường hợp này, nó sẽ chỉ trả lại tờ đầu tiên.pd.read_csv[‘File.xlsx’]] might work. But it won’t in the case of multiple sheet files as shown in the below image where there are 3 sheets[ Sheet1, Sheet2, Sheet3]. In this case, it will just return the first sheet.

Tờ Excel được sử dụng: Bấm vào đây.Click Here.

Ví dụ: Chúng ta sẽ xem cách đọc tệp Excel này.

Python3

count = len[words]
1
count = len[words]
2

count = len[words]
3
count = len[words]
4
count = len[words]
5
count = len[words]
6
count = len[words]
7
count = len[words]
4
count = len[words]
9
firsttwo = words[:2]
0

firsttwo = words[:2]
1

Output:

Bây giờ, hãy để đọc một cột đã chọn của cùng một trang:

Python3

firsttwo = words[:2]
2
count = len[words]
4
count = len[words]
5
count = len[words]
6
firsttwo = words[:2]
6

firsttwo = words[:2]
7
firsttwo = words[:2]
8
count = len[words]
4
count = len[words]
9
import codecs
if line.startswith[codecs.BOM_UTF8]:
    line = line[3:]
1
count = len[words]
4
import codecs
if line.startswith[codecs.BOM_UTF8]:
    line = line[3:]
3
firsttwo = words[:2]
0

firsttwo = words[:2]
1

Output:

Bây giờ, hãy cùng nhau đọc tất cả các trang tính:

Sheet1 chứa các cột A, B, C; Sheet2 chứa A, B, C, D và Sheet3 chứa B, D. Chúng ta sẽ thấy một ví dụ đơn giản dưới đây về cách đọc tất cả 3 tờ với nhau và hợp nhất chúng thành các cột chung.A, B, C; Sheet2 contains A, B, C, D and Sheet3 contains B, D. We will see a simple example below on how to read all the 3 sheets together and merge them into common columns.

Python3

import codecs
if line.startswith[codecs.BOM_UTF8]:
    line = line[3:]
6
count = len[words]
4
import codecs
if line.startswith[codecs.BOM_UTF8]:
    line = line[3:]
8

words = line.split[]
4
line = line.decode['utf-8']
0
line = line.decode['utf-8']
1
line = line.decode['utf-8']
2

line = line.decode['utf-8']
3
import codecs
if line.startswith[codecs.BOM_UTF8]:
    line = line[3:]
6
count = len[words]
4
line = line.decode['utf-8']
6

line = line.decode['utf-8']
7
line = line.decode['utf-8']
8
count = len[words]
4
file = codecs.open[filename, encoding='utf-8']
0
firsttwo = words[:2]
0

file = codecs.open[filename, encoding='utf-8']
2

Output:

2. Trích xuất văn bản từ hình ảnh

Bây giờ chúng tôi sẽ thảo luận về cách trích xuất văn bản từ hình ảnh.

Để cho phép chương trình Python của chúng tôi có khả năng nhận dạng ký tự, chúng tôi sẽ sử dụng thư viện PyTesseract OCR. Thư viện có thể được cài đặt vào môi trường Python của chúng tôi bằng cách thực thi lệnh sau trong trình thông dịch lệnh của HĐH:-pytesseract OCR library. The library could be installed onto our python environment by executing the following command in the command interpreter of the OS:-

pip install pytesseract

Thư viện [nếu được sử dụng trên Windows OS] yêu cầu nhị phân Tesseract.exe cũng có mặt để cài đặt thư viện thích hợp. Trong quá trình cài đặt thực thi đã nói ở trên, chúng tôi sẽ được nhắc chỉ định một đường dẫn cho nó. Đường dẫn này cần được ghi nhớ vì nó sẽ được sử dụng sau này trong mã. Đối với hầu hết các cài đặt, đường dẫn sẽ là các tệp chương trình C: \\ [x86] \\ tesseract-acr \\ tesseract.exe. & Nbsp;

Hình ảnh cho Trình diễn:

Python3

file = codecs.open[filename, encoding='utf-8']
3
file = codecs.open[filename, encoding='utf-8']
4
count = len[words]
1
file = codecs.open[filename, encoding='utf-8']
6

count = len[words]
1
file = codecs.open[filename, encoding='utf-8']
8

file = codecs.open[filename, encoding='utf-8']
9
count = len[words]
4
import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
1
import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
2
import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
3
import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
4
firsttwo = words[:2]
0

import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
6
count = len[words]
4
import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
8
import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
9

pip install pytesseract
0
pip install pytesseract
1

Output:

GeeksforGeeks

3. Trích xuất văn bản từ tệp tài liệu

Ở đây chúng tôi sẽ trích xuất văn bản từ tệp Doc bằng mô -đun DOCX.

Để cài đặt:

words = line.split[]
0

Hình ảnh cho trình diễn: & nbsp; Aniket_doc.docx & nbsp; Aniket_Doc.docx 

Ví dụ 1: Đầu tiên chúng tôi sẽ trích xuất tiêu đề:First we’ll extract the title:

Python3

count = len[words]
1
pip install pytesseract
3

pip install pytesseract
4
count = len[words]
4
pip install pytesseract
6
pip install pytesseract
7
firsttwo = words[:2]
0

pip install pytesseract
0
GeeksforGeeks
0
file = codecs.open[filename, encoding='utf-8']
0
GeeksforGeeks
2

Output:

words = line.split[]
1

Ví dụ 2: Sau đó, chúng tôi sẽ trích xuất các văn bản khác nhau có mặt [không bao gồm bảng].Then we’ll extract the different texts present[excluding the table].

Python3

GeeksforGeeks
3
count = len[words]
4
GeeksforGeeks
5
words = line.split[]
4
line = line.decode['utf-8']
0
line = line.decode['utf-8']
1
GeeksforGeeks
9
words = line.split[]
00
words = line.split[]
01
words = line.split[]
02

GeeksforGeeks
3
count = len[words]
4
words = line.split[]
05
words = line.split[]
4
line = line.decode['utf-8']
0
line = line.decode['utf-8']
1

pip install pytesseract
0
words = line.split[]
17

Output:

[Tên tôi Aniket, ‘& NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Xin chào, tôi là Aniket, ‘Tôi đang hướng dẫn về cách trích xuất văn bản từ MS Doc.

Ví dụ 3: Bây giờ chúng tôi sẽ trích xuất bảng:Now we’ll extract the table:

Python3

words = line.split[]
18
count = len[words]
4
words = line.split[]
20
file = codecs.open[filename, encoding='utf-8']
0
words = line.split[]
15

words = line.split[]
23
count = len[words]
4
words = line.split[]
25

words = line.split[]
26
count = len[words]
4
words = line.split[]
25

words = line.split[]
4
line = line.decode['utf-8']
0
line = line.decode['utf-8']
1
GeeksforGeeks
9
words = line.split[]
00
words = line.split[]
01
words = line.split[]
35

line = line.decode['utf-8']
3
words = line.split[]
4
words = line.split[]
38
line = line.decode['utf-8']
1
GeeksforGeeks
9
words = line.split[]
00
words = line.split[]
01
words = line.split[]
43

words = line.split[]
44
words = line.split[]
45
file = codecs.open[filename, encoding='utf-8']
0
GeeksforGeeks
2

line = line.decode['utf-8']
3
words = line.split[]
49

line = line.decode['utf-8']
3
words = line.split[]
51

pip install pytesseract
0
words = line.split[]
53

Output:

words = line.split[]
2

4. Trích xuất dữ liệu từ tệp PDF

Nhiệm vụ là trích xuất dữ liệu [hình ảnh, văn bản] từ PDF trong Python. Chúng tôi sẽ trích xuất các hình ảnh từ các tệp PDF và lưu chúng bằng thư viện PymupDF. Đầu tiên, chúng tôi sẽ phải cài đặt thư viện PymupDF bằng gối.

words = line.split[]
3

Ví dụ 1:

Bây giờ chúng tôi sẽ trích xuất dữ liệu từ phiên bản PDF của cùng một tệp tài liệu.

Python3

count = len[words]
1
words = line.split[]
55

words = line.split[]
56
count = len[words]
4
words = line.split[]
58
import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
    for line in f:
        line = line.lstrip[BOM]
2
words = line.split[]
00
words = line.split[]
61
firsttwo = words[:2]
0

words = line.split[]
63
count = len[words]
4
words = line.split[]
25

words = line.split[]
4
line = line.decode['utf-8']
0
line = line.decode['utf-8']
1
GeeksforGeeks
9
words = line.split[]
70

words = line.split[]
71
words = line.split[]
72
count = len[words]
4
words = line.split[]
74

words = line.split[]
71
words = line.split[]
76
count = len[words]
4
words = line.split[]
78
words = line.split[]
79
firsttwo = words[:2]
0

words = line.split[]
71
words = line.split[]
82

words = line.split[]
63
count = len[words]
4
words = line.split[]
85
words = line.split[]
86
words = line.split[]
87
words = line.split[]
88__

pip install pytesseract
0
words = line.split[]
97

Output:

[‘Tên tôi Aniket‘, ‘& NBSP;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;Xin chào, tôi là Aniket ‘, Tôi đang hướng dẫn về cách trích xuất văn bản từ MS Doc.‘, Hãy đi qua nó một cách cẩn thận.‘,‘ A ‘,‘ B ‘,‘ C, ,12

Ví dụ 2: Trích xuất hình ảnh từ PDF.Extract image from PDF.

Python3

words = line.split[]
4
words = line.split[]
99
line = line.decode['utf-8']
1
GeeksforGeeks
9
words = line.split[]
00
words = line.split[]
01
count = len[words]
04

words = line.split[]
71
words = line.split[]
4
file = codecs.open[filename, encoding='utf-8']
9
line = line.decode['utf-8']
1
count = len[words]
09

line = line.decode['utf-8']
3
count = len[words]
11
count = len[words]
4
count = len[words]
13
file = codecs.open[filename, encoding='utf-8']
0
words = line.split[]
15

line = line.decode['utf-8']
3
count = len[words]
17
count = len[words]
4
count = len[words]
19

line = line.decode['utf-8']
3
count = len[words]
21
count = len[words]
22
count = len[words]
23
count = len[words]
24

Hình ảnh được lưu trữ trong vị trí tệp hiện tại của chúng tôi như trong định dạng trang_no.-xref.png.Trong trường hợp của chúng tôi, tên của nó là trang 0-7.png.page 0-7.png.

Bây giờ, hãy để âm mưu xem hình ảnh.

Python3

count = len[words]
1
count = len[words]
26

count = len[words]
27
count = len[words]
4
count = len[words]
29
count = len[words]
30
firsttwo = words[:2]
0

count = len[words]
32

Output:


Bài Viết Liên Quan

Chủ Đề