程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python - first application (parsing text)

編輯:Python

Catalog

One 、 Study Python The feeling of

design sketch

Two 、 Code

1、 The library used

2、 The code analysis


One 、 Study Python The feeling of

On the Internet, I saw many people say that self-study Python It can perform very powerful functions in a few months , Seeing these articles, I began to doubt myself , Four months of study Python I just don't know how to use it in practice .

I didn't find the problem until the recent winter vacation , The original study is based on the content of the book , Less code , And knowledge sees more , This leads to a code shortage when learners use their ability to knock code ( I don't know how to knock it down ).

Maybe a part of learning Python Students have a lot of code typing , Also like me, I can't write a program by myself .

I deeply remember what I taught us last semester Python The teacher said to us while preparing for the final exam : This final exam will not let you do a management system , Otherwise, only single digit students in the class will pass .

Internet trends , Let many people develop in this industry , This is also my first article on code , I also believe that many students are learning Python Or other programming software , Will fall into the problem of not knowing how to knock , The only solution is : Look at the code 、 How to read 、 Knock more 、 Multiple ideas !

It would be better if there were exercises !

design sketch

Let's show it first ( There are also some flaws ), Write articles to motivate yourself , It also inspires people who are learning Python you !

Two 、 Code

1、 The library used

       wordcloud、jieba、time

2、 The code analysis

The first step is, of course, to import the required libraries

import wordcloud
import jieba
import time

About wordcloud and jieba These two third-party libraries , have access to PyCharm Of file Click Install in settings , Or into the system cmd Use :

pip install worlcloud
pip install jieba

The next step is to set parameters , Go straight to the code :

# Thesaurus
import wordcloud
import jieba
import time
class ocr:
def __init__(self) -> str: # Initializer
time.sleep(2)
print(" Welcome to the word segmentation system ")
time.sleep(0.5)
print(" Wait for the program to initialize , About need 1-3 Second ")
time.sleep(2)
def File(self, user: str) -> str: # Identify the file name , And process the data
if user[0] == "" or user[0] == " ":
print(" Input error !")
elif user[-4:] == '.txt':
file = open(user, 'r', encoding="UTF-8")
self.f = file.read()
file.close()
if user[-3:] != 'txt':
print(" Format error !")
else:
lcut = jieba.lcut(user)
self.f = " ".join(lcut)
def show(self): # Read text / File and output the cloud image
ls = jieba.lcut(self.f)
lst = []
for i in ls:
if len(i) >= 2:
lst.append(i)
aaa = str(lst)
word = wordcloud.WordCloud(
font_path="msyh.ttc",\
width=1000, height=800,\
background_color="white",\
)
word.generate(aaa)
picture_name = str(input(" Please enter a picture name :"))
word.to_file("{}.png".format(picture_name))
if __name__ == "__main__":
op = ocr()
user = input(" Please enter text / file name :\n")
op.File(user)
op.show()

There's a problem with the code , You can send it by private mail !

I'm programming Xiaobai , Please also ask the great God for advice !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved