Để đọ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]
2count = len[words]
3count = len[words]
4 count = len[words]
5count = len[words]
6count = len[words]
7count = len[words]
4 count = len[words]
9firsttwo = words[:2]
0firsttwo = words[:2]
1Output:
Bây giờ, hãy để đọc một cột đã chọn của cùng một trang:
Python3
firsttwo = words[:2]
2count = len[words]
4count = len[words]
5count = len[words]
6firsttwo = words[:2]
6firsttwo = words[:2]
7firsttwo = words[:2]
8count = len[words]
4 count = len[words]
9import codecs
if line.startswith[codecs.BOM_UTF8]:
line = line[3:]
1count = len[words]
4 import codecs
if line.startswith[codecs.BOM_UTF8]:
line = line[3:]
3firsttwo = words[:2]
0firsttwo = words[:2]
1Output:
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:]
6count = len[words]
4 import codecs
if line.startswith[codecs.BOM_UTF8]:
line = line[3:]
8words = line.split[]
4 line = line.decode['utf-8']
0line = line.decode['utf-8']
1 line = line.decode['utf-8']
2line = line.decode['utf-8']
3import codecs
if line.startswith[codecs.BOM_UTF8]:
line = line[3:]
6count = len[words]
4 line = line.decode['utf-8']
6line = line.decode['utf-8']
7line = line.decode['utf-8']
8count = len[words]
4 file = codecs.open[filename, encoding='utf-8']
0firsttwo = words[:2]
0file = codecs.open[filename, encoding='utf-8']
2Output:
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']
4count = len[words]
1 file = codecs.open[filename, encoding='utf-8']
6count = len[words]
1 file = codecs.open[filename, encoding='utf-8']
8file = codecs.open[filename, encoding='utf-8']
9count = 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]
1import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
for line in f:
line = line.lstrip[BOM]
2import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
for line in f:
line = line.lstrip[BOM]
3import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
for line in f:
line = line.lstrip[BOM]
4firsttwo = words[:2]
0import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
for line in f:
line = line.lstrip[BOM]
6count = 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]
8import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
for line in f:
line = line.lstrip[BOM]
9pip install pytesseract0
pip install pytesseract1
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[]
0Hì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 pytesseract3
pip install pytesseract4
count = len[words]
4 pip install pytesseract6
pip install pytesseract7
firsttwo = words[:2]
0pip install pytesseract0
GeeksforGeeks0
file = codecs.open[filename, encoding='utf-8']
0GeeksforGeeks2
Output:
words = line.split[]
1Ví 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
GeeksforGeeks3
count = len[words]
4GeeksforGeeks5
words = line.split[]
4 line = line.decode['utf-8']
0line = line.decode['utf-8']
1 GeeksforGeeks9
words = line.split[]
00words = line.split[]
01words = line.split[]
02GeeksforGeeks3
count = len[words]
4words = line.split[]
05words = line.split[]
4 line = line.decode['utf-8']
0line = line.decode['utf-8']
1pip install pytesseract0
words = line.split[]
17Output:
[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[]
18count = len[words]
4 words = line.split[]
20file = codecs.open[filename, encoding='utf-8']
0words = line.split[]
15words = line.split[]
23count = len[words]
4 words = line.split[]
25words = line.split[]
26count = len[words]
4 words = line.split[]
25words = line.split[]
4 line = line.decode['utf-8']
0line = line.decode['utf-8']
1 GeeksforGeeks9
words = line.split[]
00words = line.split[]
01words = line.split[]
35line = line.decode['utf-8']
3words = line.split[]
4 words = line.split[]
38line = line.decode['utf-8']
1 GeeksforGeeks9
words = line.split[]
00words = line.split[]
01words = line.split[]
43words = line.split[]
44words = line.split[]
45file = codecs.open[filename, encoding='utf-8']
0GeeksforGeeks2
line = line.decode['utf-8']
3words = line.split[]
49line = line.decode['utf-8']
3words = line.split[]
51pip install pytesseract0
words = line.split[]
53Output:
words = line.split[]
24. 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[]
3Ví 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[]
55words = line.split[]
56count = len[words]
4words = line.split[]
58import codecs
BOM = codecs.BOM_UTF8.decode['utf8']
with codecs.open[filename, encoding='utf-8'] as f:
for line in f:
line = line.lstrip[BOM]
2words = line.split[]
00words = line.split[]
61firsttwo = words[:2]
0words = line.split[]
63count = len[words]
4words = line.split[]
25words = line.split[]
4 line = line.decode['utf-8']
0line = line.decode['utf-8']
1 GeeksforGeeks9
words = line.split[]
70words = line.split[]
71words = line.split[]
72count = len[words]
4words = line.split[]
74words = line.split[]
71words = line.split[]
76count = len[words]
4words = line.split[]
78words = line.split[]
79firsttwo = words[:2]
0words = line.split[]
71words = line.split[]
82words = line.split[]
63count = len[words]
4words = line.split[]
85words = line.split[]
86words = line.split[]
87words = line.split[]
88__pip install pytesseract0
words = line.split[]
97Output:
[‘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[]
99line = line.decode['utf-8']
1 GeeksforGeeks9
words = line.split[]
00words = line.split[]
01count = len[words]
04words = line.split[]
71words = line.split[]
4 file = codecs.open[filename, encoding='utf-8']
9line = line.decode['utf-8']
1 count = len[words]
09line = line.decode['utf-8']
3count = len[words]
11count = len[words]
4count = len[words]
13file = codecs.open[filename, encoding='utf-8']
0words = line.split[]
15line = line.decode['utf-8']
3count = len[words]
17count = len[words]
4count = len[words]
19line = line.decode['utf-8']
3count = len[words]
21count = len[words]
22count = len[words]
23count = len[words]
24Hì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]
26count = len[words]
27count = len[words]
4count = len[words]
29count = len[words]
30firsttwo = words[:2]
0count = len[words]
32Output: