{"id":49775,"date":"2025-07-30T19:36:58","date_gmt":"2025-07-30T11:36:58","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/49775.html"},"modified":"2025-07-30T19:36:58","modified_gmt":"2025-07-30T11:36:58","slug":"python%e5%8c%85%e5%92%8c%e6%a8%a1%e5%9d%97day8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/49775.html","title":{"rendered":"Python\u5305\u548c\u6a21\u5757Day8"},"content":{"rendered":"<h2>\u4e00\u3001\u5305\u548c\u6a21\u5757<\/h2>\n<p>\u5728Python\u4e2d&#xff0c;\u5305\u548c\u6a21\u5757\u662f\u4ee3\u7801\u7ec4\u7ec7\u548c\u7ba1\u7406\u7684\u91cd\u8981\u6982\u5ff5\u3002\u5b83\u4eec\u5c06\u4ee3\u7801\u5206\u5272\u6210\u6613\u4e8e\u7ef4\u62a4\u548c\u590d\u7528\u7684\u7ed3\u6784\u3002<\/p>\n<h3>1. \u6a21\u5757<\/h3>\n<p>Module&#xff0c;\u6a21\u5757<\/p>\n<h4>1.1 \u57fa\u672c\u6982\u5ff5<\/h4>\n<ul>\n<li>\n<p>\u5b9a\u4e49&#xff1a;\u6a21\u5757\u662f\u4e00\u4e2aPython\u6587\u4ef6&#xff0c;\u6bcf\u4e2a $.py$ \u6587\u4ef6\u5c31\u662f\u4e00\u4e2a\u6a21\u5757\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f5c\u7528&#xff1a;\u7528\u4e8e\u7ec4\u7ec7\u4ee3\u7801&#xff0c;\u907f\u514d\u4ee3\u7801\u91cd\u590d&#xff0c;\u63d0\u9ad8\u590d\u7528\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528&#xff1a;\u901a\u8fc7 $import$ \u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>1.2 \u5feb\u901f\u4e0a\u624b<\/h4>\n<h5>1.2.1 \u5b9a\u4e49\u6a21\u5757<\/h5>\n<p>\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a $math_utils.py$ \u7684\u6a21\u5757&#xff1a;<\/p>\n<p>def add(a, b):<br \/>\n \u00a0 \u00a0return a &#043; b<br \/>\n\u200b<br \/>\ndef subtract(a, b):<br \/>\n \u00a0 \u00a0return a &#8211; b <\/p>\n<h5>1.2.2 \u5bfc\u5165\u6a21\u5757<\/h5>\n<ul>\n<li>\n<p>\u8bed\u6cd5<\/p>\n<\/li>\n<\/ul>\n<li> import \u6a21\u5757\u540d  [as \u6a21\u5757\u65b0\u540d\u5b571]\n<p>\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u5230\u5f53\u524d\u7a0b\u5e8f<\/p>\n<\/li>\n<li> from \u6a21\u5757\u540d import \u6a21\u5757\u5c5e\u6027\u540d [as \u5c5e\u6027\u65b0\u540d]\n<p>\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u5185\u90e8\u7684\u90e8\u5206\u5c5e\u6027\u5230\u5f53\u524d\u7a0b\u5e8f<\/p>\n<\/li>\n<li> from \u6a21\u5757\u540d import *\n<p>\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u5185\u90e8\u7684\u5168\u90e8\u5c5e\u6027\u5230\u5f53\u524d\u7a0b\u5e8f<\/p>\n<\/li>\n<p>import math_utils<br \/>\n\u200b<br \/>\nprint(math_utils.add(3, 5)) \u00a0# \u8f93\u51fa: 8<br \/>\nprint(math_utils.subtract(10, 4)) \u00a0# \u8f93\u51fa: 6 <\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 $from &#8230; import$ \u5bfc\u5165\u7279\u5b9a\u5185\u5bb9&#xff1a;<\/p>\n<p>from math_utils import add<br \/>\n\u200b<br \/>\nprint(add(3, 5)) \u00a0# \u8f93\u51fa: 8 <\/p>\n<h5>1.2.3 \u5185\u90e8\u5c5e\u6027<\/h5>\n<p>__file__ \u00a0\u7ed1\u5b9a\u6a21\u5757\u7684\u8def\u5f84<br \/>\n__name__ \u00a0\u7ed1\u5b9a\u6a21\u5757\u7684\u540d\u79f0<br \/>\n \u00a0 \u00a0 \u00a0 \u5982\u679c\u662f\u4e3b\u6a21\u5757&#xff08;\u9996\u5148\u542f\u52a8\u7684\u6a21\u5757&#xff09;\u5219\u7ed1\u5b9a &#039;__main__&#039;<br \/>\n \u00a0 \u00a0 \u00a0 \u5982\u679c\u4e0d\u662f\u4e3b\u6a21\u5757\u5219 \u7ed1\u5b9a xxx.py \u4e2d\u7684 xxx \u8fd9\u4e2a\u6a21\u5757\u540d <\/p>\n<h4>1.3 \u6a21\u5757\u5206\u7c7b<\/h4>\n<p>Python \u7684\u6a21\u5757\u53ef\u4ee5\u5206\u4e3a\u7cfb\u7edf\u6a21\u5757\u3001\u81ea\u5b9a\u4e49\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u6a21\u5757<\/p>\n<hr \/>\n<h5>1.3.1 \u7cfb\u7edf\u6a21\u5757<\/h5>\n<p>Pytho\u5185\u7f6e\u7684\u6a21\u5757&#xff0c;\u5b89\u88c5Python\u65f6\u5df2\u5305\u542b&#xff0c;\u63d0\u4f9b\u57fa\u7840\u529f\u80fd&#xff0c;\u6bd4\u5982\u6570\u5b66\u8fd0\u7b97\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u901a\u4fe1\u7b49\u3002<\/p>\n<p>\u7279\u70b9<\/p>\n<ul>\n<li>\n<p>\u5373\u7528\u5373\u5bfc\u5165&#xff1a;\u76f4\u63a5import\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u6027\u80fd\u9ad8\u6548&#xff1a;\u901a\u5e38\u4f7f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<\/ul>\n<h6>1.3.1.1 \u5e38\u89c1\u6a21\u5757<\/h6>\n<table>\n<tr>\u6a21\u5757\u529f\u80fd\u5b98\u65b9\u6587\u6863<\/tr>\n<tbody>\n<tr>\n<td>math<\/td>\n<td>\u6570\u5b66\u8fd0\u7b97<\/td>\n<td>math &#8212; \u6570\u5b66\u51fd\u6570 \u2014 Python 3.12.10 \u6587\u6863<\/td>\n<\/tr>\n<tr>\n<td>os<\/td>\n<td>\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3<\/td>\n<td>os &#8212; \u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3 \u2014 Python 3.13.5 \u6587\u6863<\/td>\n<\/tr>\n<tr>\n<td>os.path<\/td>\n<td>\u8def\u5f84\u76f8\u5173<\/td>\n<td>os.path &#8212; \u5e38\u7528\u7684\u8def\u5f84\u64cd\u4f5c \u2014 Python 3.13.5 \u6587\u6863<\/td>\n<\/tr>\n<tr>\n<td>datetime<\/td>\n<td>\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<td>datetime &#8212; \u57fa\u672c\u65e5\u671f\u548c\u65f6\u95f4\u7c7b\u578b \u2014 Python 3.13.5 \u6587\u6863<\/td>\n<\/tr>\n<tr>\n<td>random<\/td>\n<td>\u968f\u673a\u6570\u751f\u6210<\/td>\n<td>random &#8212; \u751f\u6210\u4f2a\u968f\u673a\u6570 \u2014 Python 3.13.5 \u6587\u6863<\/td>\n<\/tr>\n<tr>\n<td>time<\/td>\n<td>\u65f6\u95f4<\/td>\n<td>time &#8212; \u65f6\u95f4\u7684\u8bbf\u95ee\u548c\u8f6c\u6362 \u2014 Python 3.13.5 \u6587\u6863<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h6>1.3.1.2 \u65b9\u6cd5\u793a\u4f8b<\/h6>\n<p>random\u6a21\u5757&#xff1a;<\/p>\n<p>import random<br \/>\nrandom.randint(1, 6) \u00a0# random.randint(a,b) \u751f\u4ea7 a~b\u7684\u968f\u673a\u6574\u6570<br \/>\nrandom.randint(1, 6)<br \/>\nrandom.random() \u00a0 # random.random  \u751f\u6210\u5305\u542b0 \u4f46\u4e0d\u5305\u542b1 \u7684\u6d6e\u70b9\u6570<br \/>\nrandom.choice(&#034;ABCD&#034;) \u00a0 \u00a0# \u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d&#xff0c;\u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u5143\u7d20<br \/>\nrandom.choice(&#034;ABCD&#034;)<br \/>\nL &#061; [1, 2, 3, 6, 9]<br \/>\nrandom.choice(L)<br \/>\nrandom.shuffle(L) \u00a0 # random.shuffer(x)  # \u628a\u5217\u8868X \u6253\u4e71<br \/>\nprint(L)<br \/>\n\u200b <\/p>\n<p>\u968f\u673a\u751f\u6210\u5bc6\u7801&#xff1a;<\/p>\n<p>import random<br \/>\nimport time<br \/>\n\u200b<br \/>\n\u200b<br \/>\n## \u751f\u6210 n \u4f4d\u7684\u6570\u5b57\u5bc6\u7801<br \/>\ndef get_random_password(n):<br \/>\n \u00a0 \u00a0r_str &#061; &#039;&#039;<br \/>\n \u00a0 \u00a0for _ in range(n):<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0r_str &#043;&#061; str(random.randint(0, 9))<br \/>\n \u00a0 \u00a0return r_str<br \/>\n\u200b<br \/>\n\u200b<br \/>\n## \u751f\u6210 n \u4f4d\u7684\u6570\u5b57\u548c\u5b57\u6bcd\u7ec4\u6210\u7684\u5bc6\u7801<br \/>\ncharactors &#061; &#039;0123456789abcdefghijklmnopqrstwuxyz&#039;<br \/>\n\u200b<br \/>\n\u200b<br \/>\ndef get_random_password2(n):<br \/>\n \u00a0 \u00a0r_str &#061; &#039;&#039;<br \/>\n \u00a0 \u00a0for _ in range(n):<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0r_str &#043;&#061; random.choice(charactors)<br \/>\n \u00a0 \u00a0return r_str<br \/>\n\u200b<br \/>\n\u200b<br \/>\nprint(get_random_password(6))<br \/>\nprint(get_random_password(6))<br \/>\n\u200b<br \/>\ntime.sleep(2)<br \/>\n\u200b<br \/>\nprint(get_random_password2(10)) <\/p>\n<p>time\u6a21\u5757&#xff1a;<\/p>\n<p>import time<br \/>\n\u200b<br \/>\ntime.time() \u00a0# \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233<br \/>\ntime.ctime() \u00a0#\u8fd4\u56de\u5f53\u524d\u7684UTC \u65f6\u95f4\u7684\u5b57\u7b26\u4e32<br \/>\nt1 &#061; time.localtime() \u00a0# \u8fd4\u56de\u5f53\u524d\u7684\u672c\u5730\u65f6\u95f4\u5143\u7ec4<br \/>\ntime.sleep(3) \u00a0# \u8ba9\u7a0b\u5e8f\u7761\u7720 n \u79d2<br \/>\ntime.strftime(&#034;%Y-%m-%d&#034;, t1) \u00a0# \u683c\u5f0f\u5316\u65f6\u95f4<br \/>\ntime.strftime(&#034;%y-%m-%d&#034;, t1)<br \/>\ntime.strftime(&#039;%Y-%m-%d %H:%M:%S&#039;, t1) <\/p>\n<p>os\u6a21\u5757&#xff1a;<\/p>\n<p># 1. os.getcwd(): \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<br \/>\nimport os<br \/>\ncurrent_directory &#061; os.getcwd()<br \/>\nprint(&#034;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&#034;, current_directory)<br \/>\n\u200b<br \/>\n# 2. os.chdir(path): \u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<br \/>\nnew_directory &#061; &#034;F:\\\\\\\\01.AI07.\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\\\\\\\\00.\u4e0a\u8bfe\u8bfe\u4ef6&#034;<br \/>\nos.chdir(new_directory)<br \/>\nprint(&#034;\u5de5\u4f5c\u76ee\u5f55\u5df2\u66f4\u6539\u4e3a:&#034;, os.getcwd())<br \/>\n\u200b<br \/>\n# 3. os.listdir(path&#061;&#039;.&#039;): \u8fd4\u56de\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u5217\u8868<br \/>\ndirectory_path &#061; &#034;.&#034;<br \/>\nfiles_and_dirs &#061; os.listdir(directory_path)<br \/>\nprint(&#034;\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u5217\u8868:&#034;, files_and_dirs)<br \/>\n\u200b<br \/>\n# 4. os.mkdir(path): \u521b\u5efa\u76ee\u5f55<br \/>\nnew_directory &#061; &#034;new_folder&#034;<br \/>\nos.mkdir(new_directory)<br \/>\nprint(f&#034;\u76ee\u5f55 &#039;{new_directory}&#039; \u5df2\u521b\u5efa&#034;)<br \/>\n\u200b<br \/>\n# 5. os.rmdir(path): \u5220\u9664\u76ee\u5f55<br \/>\ndirectory_to_remove &#061; &#034;new_folder&#034;<br \/>\nos.rmdir(directory_to_remove)<br \/>\nprint(f&#034;\u76ee\u5f55 &#039;{directory_to_remove}&#039; \u5df2\u5220\u9664&#034;)<br \/>\n\u200b<br \/>\n# 6. os.remove(path): \u5220\u9664\u6587\u4ef6<br \/>\nfile_to_remove &#061; &#034;example.txt&#034;<br \/>\nos.remove(file_to_remove)<br \/>\nprint(f&#034;\u6587\u4ef6 &#039;{file_to_remove}&#039; \u5df2\u5220\u9664&#034;) <\/p>\n<h5>1.3.2 \u7b2c\u4e09\u65b9\u6a21\u5757<\/h5>\n<p>\u7531\u793e\u533a\u6216\u516c\u53f8\u5f00\u53d1&#xff0c;\u901a\u8fc7\u5305\u7ba1\u7406\u5de5\u5177&#xff08;\u5982pip &#xff09;\u5b89\u88c5&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u66f4\u9ad8\u7ea7\u6216\u66f4\u5177\u4f53\u7684\u529f\u80fd&#xff0c;\u5982\u6570\u636e\u79d1\u5b66\u3001\u56fe\u50cf\u5904\u7406\u3001\u7f51\u7edc\u722c\u866b\u7b49\u3002<\/p>\n<h6>1.3.2.1 \u5e38\u89c1\u6a21\u5757<\/h6>\n<p>\u90a3\u8fd9\u53ef\u5c31\u592a\u591a\u4e86~\u8fd9\u91cc\u968f\u4fbf\u4e3e\u7684\u4f8b\u5b50<\/p>\n<table>\n<tr>\u6a21\u5757\u529f\u80fd\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>numpy<\/td>\n<td>\u6570\u503c\u8ba1\u7b97<\/td>\n<td>\u79d1\u5b66\u8fd0\u7b97\u3001\u77e9\u9635\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>pandas<\/td>\n<td>\u6570\u636e\u5206\u6790<\/td>\n<td>\u6570\u636e\u6e05\u6d17\u4e0e\u5904\u7406<\/td>\n<\/tr>\n<tr>\n<td>matplotlib<\/td>\n<td>\u6570\u636e\u53ef\u89c6\u5316<\/td>\n<td>\u7ed8\u5236\u56fe\u8868<\/td>\n<\/tr>\n<tr>\n<td>requests<\/td>\n<td>HTTP\u8bf7\u6c42\u5904\u7406<\/td>\n<td>\u722c\u53d6\u7f51\u9875\u5185\u5bb9&#xff0c;\u8c03\u7528API<\/td>\n<\/tr>\n<tr>\n<td>flask<\/td>\n<td>Web\u5e94\u7528\u6846\u67b6<\/td>\n<td>\u5feb\u901f\u642d\u5efaWeb\u670d\u52a1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h6>1.3.2.2 \u4f7f\u7528\u793a\u4f8b<\/h6>\n<li>\n<p>\u5b89\u88c5\u6a21\u5757&#xff1a;<\/p>\n<p> pip install numpy pandas requests <\/li>\n<li>\n<p>\u4f7f\u7528&#xff1a;<\/p>\n<p> # numpy \u6a21\u5757<br \/>\nimport numpy as np<br \/>\narray &#061; np.array([1, 2, 3])<br \/>\nprint(array.mean()) \u00a0# \u8f93\u51fa: \u5e73\u5747\u503c 2.0<br \/>\n\u200b<br \/>\n# pandas \u6a21\u5757<br \/>\nimport pandas as pd<br \/>\ndf &#061; pd.DataFrame({&#034;Name&#034;: [&#034;Alice&#034;, &#034;Bob&#034;], &#034;Age&#034;: [25, 30]})<br \/>\nprint(df)<br \/>\n\u200b<br \/>\n# requests \u6a21\u5757<br \/>\nimport requests<br \/>\nresponse &#061; requests.get(&#034;https:\/\/jsonplaceholder.typicode.com\/posts\/1&#034;)<br \/>\nprint(response.json()) <\/li>\n<h5>1.3.3 \u81ea\u5b9a\u4e49\u6a21\u5757<\/h5>\n<ul>\n<li>\n<p>\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u6a21\u5757&#xff0c;\u7528\u4e8e\u590d\u7528\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u540d\u4ee5 $.py$ \u7ed3\u5c3e&#xff0c;\u5b58\u653e\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3001\u7c7b\u7b49\u3002<\/p>\n<\/li>\n<\/ul>\n<h6>1.3.3.1 \u521b\u5efa\u6a21\u5757<\/h6>\n<p>\u521b\u5efa\u4e00\u4e2autils.py\u6587\u4ef6&#xff1a;<\/p>\n<p># utils.py<br \/>\ndef add(a, b):<br \/>\n \u00a0 \u00a0return a &#043; b<br \/>\n\u200b<br \/>\ndef greet(name):<br \/>\n \u00a0 \u00a0return f&#034;Hello, {name}!&#034; <\/p>\n<h6>1.3.3.2 \u4f7f\u7528\u6a21\u5757<\/h6>\n<p>\u521b\u5efa\u4e00\u4e2amain.py\u6587\u4ef6&#xff0c;\u5e76\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757&#xff1a;<\/p>\n<p># main.py<br \/>\nimport utils<br \/>\n\u200b<br \/>\nprint(utils.add(3, 5)) \u00a0# \u8f93\u51fa: 8<br \/>\nprint(utils.greet(&#034;Alice&#034;)) \u00a0# \u8f93\u51fa: Hello, Alice! <\/p>\n<h6>1.3.3.3 \u8de8\u6587\u4ef6\u5939\u4f7f\u7528<\/h6>\n<p>\u5047\u8bbe\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b&#xff1a;<\/p>\n<p>project\/<br \/>\n \u00a0  utils\/<br \/>\n \u00a0 \u00a0 \u00a0  __init__.py<br \/>\n \u00a0 \u00a0 \u00a0  math_utils.py<br \/>\n \u00a0  main.py <\/p>\n<p>\u5728main.py\u4e2d&#xff1a;<\/p>\n<p>from utils.math_utils import add<br \/>\n\u200b<br \/>\nprint(add(3, 5)) \u00a0# \u8f93\u51fa: 8 <\/p>\n<hr \/>\n<h5>1.3.4 \u603b\u7ed3\u5bf9\u6bd4<\/h5>\n<table>\n<tr>\u5206\u7c7b\u83b7\u53d6\u65b9\u5f0f\u4f7f\u7528\u573a\u666f\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\u7cfb\u7edf\u6a21\u5757<\/td>\n<td>\u968fPython\u81ea\u5e26<\/td>\n<td>\u5e38\u7528\u529f\u80fd&#xff0c;\u6027\u80fd\u9ad8\u6548<\/td>\n<td>math, os, random<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c\u4e09\u65b9\u6a21\u5757<\/td>\n<td>pip \u5b89\u88c5<\/td>\n<td>\u9ad8\u7ea7\u529f\u80fd&#xff0c;\u7279\u5b9a\u9886\u57df<\/td>\n<td>numpy, requests<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u5b9a\u4e49\u6a21\u5757<\/td>\n<td>\u81ea\u5df1\u7f16\u5199<\/td>\n<td>\u9879\u76ee\u7279\u5b9a\u9700\u6c42&#xff0c;\u7075\u6d3b\u63a7\u5236<\/td>\n<td>utils.py<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2. \u5305<\/h3>\n<p>Package<\/p>\n<h4>2.1 \u57fa\u672c\u6982\u5ff5<\/h4>\n<ul>\n<li>\n<p>\u5b9a\u4e49&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\u3002<\/p>\n<\/li>\n<li>\n<p>\u5305\u7684\u672c\u8d28\u662f\u4e00\u4e2a\u5305\u542b $__init__.py$ \u6587\u4ef6\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6 $__init__.py$ \u53ef\u4e3a\u7a7a&#xff0c;\u4e5f\u53ef\u4ee5\u5305\u542b\u521d\u59cb\u5316\u4ee3\u7801\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4f5c\u7528&#xff1a;\u7528\u4e8e\u7ec4\u7ec7\u6a21\u5757\u7684\u96c6\u5408&#xff0c;\u5f62\u6210\u5c42\u6b21\u5316\u7684\u7ed3\u6784&#xff0c;\u4fbf\u4e8e\u7ba1\u7406\u5927\u578b\u9879\u76ee\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ed3\u6784&#xff1a;<\/p>\n<p> my_package\/<br \/>\n \u00a0  __init__.py<br \/>\n \u00a0  module1.py<br \/>\n \u00a0  module2.py<br \/>\n \u00a0  subpackage\/<br \/>\n \u00a0 \u00a0 \u00a0  __init__.py<br \/>\n \u00a0 \u00a0 \u00a0  module3.py <\/li>\n<\/ul>\n<h4>2.2 \u5bfc\u5165\u5305\u548c\u5b50\u5305<\/h4>\n<ul>\n<li>\n<p>\u4f7f\u7528import\u5173\u952e\u5b57\u53ef\u4ee5\u5bfc\u5165\u5305\u548c\u5b50\u5305&#xff0c;\u4ee5\u8bbf\u95ee\u5176\u4e2d\u7684\u6a21\u5757\u548c\u5185\u5bb9\u3002<\/p>\n<\/li>\n<\/ul>\n<p> \u00a0 \u00a0# \u540c\u6a21\u5757\u7684\u5bfc\u5165\u89c4\u5219<br \/>\n \u00a0 \u00a0import \u5305\u540d [as \u5305\u522b\u540d]<br \/>\n \u00a0 \u00a0import \u00a0\u5305\u540d.\u6a21\u5757\u540d [as \u6a21\u5757\u65b0\u540d]<br \/>\n \u00a0 \u00a0import \u00a0\u5305\u540d.\u5b50\u5305\u540d.\u6a21\u5757\u540d [as \u6a21\u5757\u65b0\u540d]<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0from \u5305\u540d import \u6a21\u5757\u540d [as \u6a21\u5757\u65b0\u540d]<br \/>\n \u00a0 \u00a0from \u5305\u540d.\u5b50\u5305\u540d import \u6a21\u5757\u540d [as \u6a21\u5757\u65b0\u540d]<br \/>\n \u00a0 \u00a0from \u5305\u540d.\u5b50\u5305\u540d.\u6a21\u5757\u540d import \u5c5e\u6027\u540d [as \u5c5e\u6027\u65b0\u540d]<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0# \u5bfc\u5165\u5305\u5185\u7684\u6240\u6709\u5b50\u5305\u548c\u6a21\u5757<br \/>\n \u00a0 \u00a0from \u5305\u540d import *<br \/>\n \u00a0 \u00a0from \u5305\u540d.\u6a21\u5757\u540d import * <\/p>\n<p>\u53c2\u8003\u6848\u4f8b&#xff1a;<\/p>\n<p># \u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757<br \/>\nimport matplotlib.pyplot as plt<br \/>\n\u200b<br \/>\n# \u5bfc\u5165\u5b50\u5305\u4e2d\u7684\u6a21\u5757<br \/>\nfrom sklearn.linear_model import LinearRegression <\/p>\n<h4>2.4 __init__.py\u6587\u4ef6<\/h4>\n<p>__init__.py \u7684\u4f5c\u7528<\/p>\n<li>\n<p>\u6807\u8bc6\u5305&#xff1a;<\/p>\n<ul>\n<li>\n<p>__init__.py \u6587\u4ef6\u7684\u5b58\u5728\u6807\u5fd7\u7740\u8be5\u76ee\u5f55\u662f\u4e00\u4e2a Python \u5305\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u6ca1\u6709 __init__.py \u6587\u4ef6&#xff0c;Python \u4f1a\u5c06\u76ee\u5f55\u89c6\u4e3a\u666e\u901a\u76ee\u5f55&#xff0c;\u65e0\u6cd5\u901a\u8fc7\u5305\u7684\u65b9\u5f0f\u5bfc\u5165\u5176\u4e2d\u7684\u6a21\u5757\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u521d\u59cb\u5316\u5305&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5f53\u5305\u6216\u5305\u4e2d\u7684\u6a21\u5757\u88ab\u5bfc\u5165\u65f6&#xff0c;__init__.py \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u4f1a\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u5728\u8fd9\u91cc\u7f16\u5199\u5305\u7684\u521d\u59cb\u5316\u903b\u8f91&#xff0c;\u4f8b\u5982\u8bbe\u7f6e\u5305\u7ea7\u522b\u7684\u53d8\u91cf\u3001\u5bfc\u5165\u5b50\u6a21\u5757\u6216\u6267\u884c\u5176\u4ed6\u521d\u59cb\u5316\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5b9a\u4e49\u5305\u7684\u63a5\u53e3&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u53ef\u4ee5\u5728 __init__.py \u4e2d\u5b9a\u4e49 __all__ \u53d8\u91cf&#xff0c;\u660e\u786e\u6307\u5b9a\u901a\u8fc7 from package import * \u5bfc\u5165\u65f6\u54ea\u4e9b\u6a21\u5757\u6216\u5bf9\u8c61\u662f\u516c\u5f00\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e5f\u53ef\u4ee5\u5728 __init__.py \u4e2d\u5bfc\u5165\u5305\u5185\u7684\u6a21\u5757\u6216\u5b50\u5305&#xff0c;\u7b80\u5316\u7528\u6237\u7684\u5bfc\u5165\u65b9\u5f0f\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u547d\u540d\u7a7a\u95f4\u7ba1\u7406&#xff1a;<\/p>\n<ul>\n<li>\n<p>__init__.py \u6587\u4ef6\u53ef\u4ee5\u5e2e\u52a9\u7ba1\u7406\u5305\u7684\u547d\u540d\u7a7a\u95f4&#xff0c;\u786e\u4fdd\u5305\u5185\u7684\u6a21\u5757\u548c\u5b50\u5305\u80fd\u591f\u6b63\u786e\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<p>__all__ \u7684\u4f5c\u7528<\/p>\n<li>\n<p>\u63a7\u5236 from module import \\\\* \u7684\u884c\u4e3a&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;from module import * \u4f1a\u5bfc\u5165\u6a21\u5757\u4e2d\u6240\u6709\u4e0d\u4ee5 _ \u5f00\u5934\u7684\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5b9a\u4e49\u4e86 __all__&#xff0c;\u5219\u53ea\u6709 __all__ \u4e2d\u5217\u51fa\u7684\u5bf9\u8c61\u4f1a\u88ab\u5bfc\u5165\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u660e\u786e\u516c\u5f00\u63a5\u53e3&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u901a\u8fc7 __all__&#xff0c;\u5f00\u53d1\u8005\u53ef\u4ee5\u660e\u786e\u544a\u8bc9\u7528\u6237\u54ea\u4e9b\u5bf9\u8c61\u662f\u6a21\u5757\u6216\u5305\u7684\u516c\u5f00\u63a5\u53e3&#xff0c;\u54ea\u4e9b\u662f\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd9\u662f\u4e00\u79cd\u826f\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5&#xff0c;\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u9632\u6b62\u610f\u5916\u5bfc\u5165&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u6ca1\u6709 __all__&#xff0c;from module import * \u53ef\u80fd\u4f1a\u5bfc\u5165\u4e00\u4e9b\u4e0d\u5e0c\u671b\u66b4\u9732\u7684\u5185\u90e8\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u901a\u8fc7 __all__&#xff0c;\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<p>\u793a\u4f8b1&#xff1a;\u5728 __init__.py \u4e2d\u5b9a\u4e49\u53d8\u91cf<\/p>\n<p>\u53ef\u4ee5\u5728 __init__.py \u4e2d\u5b9a\u4e49\u5305\u7ea7\u522b\u7684\u53d8\u91cf<\/p>\n<p>version &#061; &#034;1.0.0&#034; <\/p>\n<p>\u5728\u5176\u5b83\u6a21\u5757\u4f7f\u7528\u65f6&#xff1a;<\/p>\n<p>import my_package<br \/>\nprint(my_package.version) \u00a0# \u8f93\u51fa: 1.0.0 <\/p>\n<p>\u793a\u4f8b2&#xff1a;\u5728 __init__.py \u4e2d\u5bfc\u5165\u6a21\u5757<\/p>\n<p>\u53ef\u4ee5\u5728 __init__.py \u4e2d\u5bfc\u5165\u5305\u5185\u7684\u6a21\u5757&#xff0c;\u7b80\u5316\u7528\u6237\u7684\u5bfc\u5165\u65b9\u5f0f<\/p>\n<p># __init__.py<br \/>\nfrom .module1 import func1<br \/>\nfrom .module2 import func2 <\/p>\n<p>\u5728\u5176\u5b83\u6a21\u5757\u4f7f\u7528\u65f6&#xff1a;<\/p>\n<p>from my_package import func1, func2<br \/>\nfunc1()<br \/>\nfunc2() <\/p>\n<p>\u793a\u4f8b3&#xff1a;\u4f7f\u7528 __all__ \u5b9a\u4e49\u516c\u5f00\u63a5\u53e3<\/p>\n<p>\u53ef\u4ee5\u5728 __init__.py \u4e2d\u5b9a\u4e49 __all__&#xff0c;\u660e\u786e\u6307\u5b9a\u901a\u8fc7 from package import * \u5bfc\u5165\u65f6\u54ea\u4e9b\u5bf9\u8c61\u662f\u516c\u5f00\u7684<\/p>\n<p># __init__.py<br \/>\n__all__ &#061; [&#034;func1&#034;, &#034;func2&#034;]<br \/>\n\u200b<br \/>\nfrom .module1 import func1<br \/>\nfrom .module2 import func2 <\/p>\n<p>\u4f7f\u7528\u65f6&#xff1a;<\/p>\n<p>from my_package import *<br \/>\nfunc1()<br \/>\nfunc2() <\/p>\n<h3>3. \u6a21\u5757\u4e0e\u5305\u7684\u533a\u522b<\/h3>\n<table>\n<tr>\u6a21\u5757\u5305<\/tr>\n<tbody>\n<tr>\n<td>\u4e00\u4e2aPython\u6587\u4ef6<\/td>\n<td>\u4e00\u4e2a\u5305\u542b__init__.py\u7684\u6587\u4ef6\u5939<\/td>\n<\/tr>\n<tr>\n<td>\u63d0\u4f9b\u57fa\u672c\u529f\u80fd\u5355\u5143<\/td>\n<td>\u7528\u4e8e\u7ec4\u7ec7\u591a\u4e2a\u6a21\u5757<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u4ef6\u540d\u4e3a.py<\/td>\n<td>\u76ee\u5f55\u540d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e8c\u3001Python\u6587\u4ef6\u64cd\u4f5c<\/h2>\n<p>\u6587\u4ef6\u64cd\u4f5c\u662fPython\u4e2d\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00&#xff0c;\u7528\u4e8e\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u5199\u5165\u548c\u7ba1\u7406\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6587\u4ef6\u64cd\u4f5c\u4efb\u52a1\u7684\u601d\u8def\u3001\u603b\u7ed3\u548c\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<h3>1. \u6253\u5f00\u6587\u4ef6<\/h3>\n<p>\u8981\u6267\u884c\u6587\u4ef6\u64cd\u4f5c&#xff0c;\u9996\u5148\u9700\u8981\u6253\u5f00\u6587\u4ef6\u3002\u4f7f\u7528open()\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6&#xff0c;\u6307\u5b9a\u6587\u4ef6\u540d\u4ee5\u53ca\u6253\u5f00\u6a21\u5f0f&#xff08;\u8bfb\u53d6\u3001\u5199\u5165\u3001\u8ffd\u52a0\u7b49&#xff09;\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"780\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730113655-688a03d74a6a5.png\" width=\"1288\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"836\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730113656-688a03d85eeb2.png\" width=\"1275\" \/><\/p>\n<h3>2. \u8bfb\u53d6\u6587\u4ef6<\/h3>\n<p>\u4e00\u65e6\u6587\u4ef6\u88ab\u6253\u5f00&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p># \u6253\u5f00\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u4ee5\u8bfb\u53d6\u5185\u5bb9<br \/>\nfile &#061; open(&#034;example.txt&#034;, &#034;r&#034;)<br \/>\n# \u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9<br \/>\ncontent &#061; file.read()<br \/>\n\u200b<br \/>\n# \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<br \/>\nfor line in file: \u00a0#\u76f4\u63a5\u904d\u5386\u6587\u4ef6\u5bf9\u8c61&#xff0c;\u6bcf\u6b21\u8bfb\u53d6\u4e00\u884c\u3002\u8fd9\u79cd\u65b9\u5f0f\u66f4\u5185\u5b58\u53cb\u597d&#xff0c;\u56e0\u4e3a\u4e0d\u9700\u8981\u5c06\u6240\u6709\u884c\u8bfb\u5165\u5185\u5b58\u3002<br \/>\n \u00a0 \u00a0print(line)<br \/>\nwith open(&#039;example.txt&#039;, &#039;r&#039;) as file:<br \/>\n \u00a0 \u00a0lines &#061; file.readlines() # \u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c&#xff0c;\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5217\u8868\u8fd4\u56de\u3002<br \/>\n \u00a0 \u00a0for line in lines:<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0print(line, end&#061;&#039;&#039;) \u4ee3\u7801\u548cfile &#061; open(&#034;example.txt&#034;, &#034;r&#034;)for line in file:<br \/>\n \u00a0 \u00a0print(line) \u4ee3\u7801\u7684\u533a\u522b <\/p>\n<h3>3. \u5199\u5165\u6587\u4ef6<\/h3>\n<p>\u8981\u5199\u5165\u6587\u4ef6&#xff0c;\u9700\u8981\u6253\u5f00\u6587\u4ef6\u4ee5\u5199\u5165\u6a21\u5f0f&#xff08;&#039;w&#039;&#xff09;&#xff0c;\u7136\u540e\u4f7f\u7528write()\u65b9\u6cd5\u3002<\/p>\n<p># \u6253\u5f00\u6587\u4ef6\u4ee5\u5199\u5165\u5185\u5bb9<br \/>\nfile &#061; open(&#034;example.txt&#034;, &#034;w&#034;)<br \/>\n\u200b<br \/>\n# \u5199\u5165\u5185\u5bb9<br \/>\nfile.write(&#034;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\u3002&#034;) <\/p>\n<h3>4. \u5173\u95ed\u6587\u4ef6<\/h3>\n<p>\u5b8c\u6210\u6587\u4ef6\u64cd\u4f5c\u540e&#xff0c;\u5e94\u8be5\u5173\u95ed\u6587\u4ef6&#xff0c;\u4ee5\u91ca\u653e\u8d44\u6e90\u548c\u786e\u4fdd\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>file.close() <\/p>\n<h3>5. \u4f7f\u7528with<\/h3>\n<p>\u66f4\u5b89\u5168\u7684\u65b9\u6cd5\u662f\u4f7f\u7528with\u8bed\u53e5&#xff0c;\u5b83\u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<p>with open(&#034;example.txt&#034;, &#034;r&#034;) as file:<br \/>\n \u00a0 \u00a0content &#061; file.read()<br \/>\n \u00a0 \u00a0# \u6587\u4ef6\u81ea\u52a8\u5173\u95ed <\/p>\n<h3>6. \u68c0\u67e5\u662f\u5426\u5b58\u5728<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528os.path.exists()\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p>import os<br \/>\nif os.path.exists(&#034;example.txt&#034;):<br \/>\n \u00a0 \u00a0print(&#034;\u6587\u4ef6\u5b58\u5728&#034;) <\/p>\n<h2>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26\u5e8f\u5217&#xff0c;\u5b83\u80fd\u5e2e\u52a9\u4f60\u65b9\u4fbf\u7684\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e0e\u67d0\u79cd\u6a21\u5f0f\u5339\u914d\u3002re\u6a21\u5757\u4f7fPython\u62e5\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u3002<\/p>\n<h3>1. \u5b57\u7b26\u5339\u914d<\/h3>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7f51\u7edc\u722c\u866b\u3001\u6570\u636e\u5206\u6790\u4e2d\u6709\u7740\u5e7f\u6cdb\u4f7f\u7528&#xff0c;\u638c\u63e1\u6b63\u5219\u8868\u8fbe\u5f0f\u80fd\u591f\u8fbe\u5230\u4e8b\u534a\u529f\u500d\u7684\u6548\u679c\u3002<\/p>\n<h4>1.1 \u65b9\u6cd5\u548c\u529f\u80fd<\/h4>\n<table>\n<tr>\u65b9\u6cd5\u529f\u80fd<\/tr>\n<tbody>\n<tr>\n<td>match()<\/td>\n<td>\u5224\u65ad\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5426\u4ece\u5f00\u59cb\u5904\u5339\u914d\u4e00\u4e2a\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>search()<\/td>\n<td>\u904d\u5386\u5b57\u7b26\u4e32&#xff0c;\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u8fd4\u56de\u5339\u914d\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>findall()<\/td>\n<td>\u904d\u5386\u5b57\u7b26\u4e32&#xff0c;\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u6240\u6709\u4f4d\u7f6e&#xff0c;\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u5982\u679c\u7ed9\u51fa\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u5305\u542b\u5b50\u7ec4&#xff0c;\u5c31\u4f1a\u628a\u5b50\u7ec4\u7684\u5185\u5bb9\u5355\u72ec\u8fd4\u56de&#xff0c;\u5982\u679c\u6709\u591a\u4e2a\u5b50\u7ec4\u5c31\u4f1a\u4ee5\u5143\u7ec4\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<\/td>\n<\/tr>\n<tr>\n<td>finditer()<\/td>\n<td>\u904d\u5386\u5b57\u7b26\u4e32&#xff0c;\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u6240\u6709\u4f4d\u7f6e&#xff0c;\u5e76\u4ee5\u8fed\u4ee3\u5668\u7684\u5f62\u5f0f\u8fd4\u56de<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\n<p>hqyj\u5339\u914d\u6587\u672c\u4e2d\u7684hqyj<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\ntext&#061;&#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08\u4e5f\u725b\u76ae666&#034;<br \/>\ndata&#061;re.findall(&#034;hqyj&#034;,text)<br \/>\nprint(data)#[&#039;hqyj&#039;, &#039;hqyj&#039;] <\/p>\n<ul>\n<li>\n<p>[hqyj]\u5339\u914dh\u6216\u8005q\u6216\u8005y\u6216\u8005j\u5b57\u7b26<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\ntext&#061;&#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08\u4e5f\u725b\u76ae666&#034;<br \/>\ndata&#061;re.findall(&#034;[hqyj]&#034;,text)<br \/>\nprint(data)#[&#039;h&#039;, &#039;q&#039;, &#039;y&#039;, &#039;j&#039;, &#039;h&#039;, &#039;q&#039;, &#039;y&#039;, &#039;j&#039;]<br \/>\nimport re<br \/>\ntext&#061;&#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08\u4e5f\u725b\u76ae666&#034;<br \/>\ndata&#061;re.findall(&#034;[hqyj]\u725b&#034;,text)<br \/>\nprint(data)#[&#039;j\u725b&#039;] <\/p>\n<ul>\n<li>\n<p>[^hqyj]\u5339\u914d\u9664\u4e86hqyj\u4ee5\u5916\u7684\u5176\u4ed6\u5b57\u7b26<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\ntext&#061;&#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08\u4e5f\u725b\u76ae666&#034;<br \/>\ndata&#061;re.findall(&#034;[^hqyj]&#034;,text)<br \/>\nprint(data)#[&#039;\u725b&#039;, &#039;\u76ae&#039;, &#039;6&#039;, &#039;6&#039;, &#039;6&#039;, &#039;6&#039;, &#039;,&#039;, &#039;\u6709&#039;, &#039;\u4e2a&#039;, &#039;\u8001&#039;, &#039;\u5e08&#039;, &#039;\u4e5f&#039;, &#039;\u725b&#039;, &#039;\u76ae&#039;, &#039;6&#039;, &#039;6&#039;, &#039;6&#039;]<br \/>\n\u200b <\/p>\n<ul>\n<li>\n<p>[a-z]\u5339\u914da~z\u7684\u4efb\u610f\u5b57\u7b26([0-9]\u4e5f\u53ef\u4ee5)<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08abchqyj\u4e5f\u725b\u76ae666&#034;<br \/>\ndata &#061; re.findall(&#034;[a-z]hqyj&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;chqyj&#039;] <\/p>\n<ul>\n<li>\n<p>.\u5339\u914d\u9664\u4e86\u6362\u884c\u7b26\u4ee5\u5916\u7684\u4efb\u610f\u5b57\u7b26<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\ntext&#061;&#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08abchqyj\u4e5f\u725b\u76ae666&#034;<br \/>\ndata&#061;re.findall(&#034;.hqyj&#034;,text)<br \/>\nprint(data)#[&#039;,hqyj&#039;, &#039;chqyj&#039;]<br \/>\nimport re<br \/>\ntext&#061;&#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08abchqyj\u4e5f\u725b\u76ae666&#034;<br \/>\ndata&#061;re.findall(&#034;.&#043;hqyj&#034;,text) #\u8d2a\u5a6a\u5339\u914d(\u5339\u914d\u6700\u957f\u7684)<br \/>\nprint(data)#[&#039;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08abchqyj&#039;]<br \/>\nimport re<br \/>\ntext&#061;&#034;hqyj\u725b\u76ae6666,hqyj\u6709\u4e2a\u8001\u5e08abchqyj\u4e5f\u725b\u76ae666&#034;<br \/>\ndata&#061;re.findall(&#034;.?hqyj&#034;,text)<br \/>\nprint(data)#[&#039;hqyj&#039;, &#039;,hqyj&#039;, &#039;chqyj&#039;] <\/p>\n<h4>1.2 \u7279\u6b8a\u5b57\u7b26<\/h4>\n<table>\n<tr>\u7279\u6b8a\u5b57\u7b26\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>\\\\d<\/td>\n<td>\u5339\u914d\u4efb\u4f55\u5341\u8fdb\u5236\u6570\u5b57&#xff1b;\u76f8\u5f53\u4e8e\u7c7b [0-9]<\/td>\n<\/tr>\n<tr>\n<td>\\\\D<\/td>\n<td>\u4e0e \\\\d \u76f8\u53cd&#xff0c;\u5339\u914d\u4efb\u4f55\u975e\u5341\u8fdb\u5236\u6570\u5b57\u7684\u5b57\u7b26&#xff1b;\u76f8\u5f53\u4e8e\u7c7b 0-9<\/td>\n<\/tr>\n<tr>\n<td>\\\\s<\/td>\n<td>\u5339\u914d\u4efb\u4f55\u7a7a\u767d\u5b57\u7b26&#xff08;\u5305\u542b\u7a7a\u683c\u3001\u6362\u884c\u7b26\u3001\u5236\u8868\u7b26\u7b49&#xff09;&#xff1b;\u76f8\u5f53\u4e8e\u7c7b [ \\\\t\\\\n\\\\r\\\\f\\\\v]<\/td>\n<\/tr>\n<tr>\n<td>\\\\S<\/td>\n<td>\u4e0e \\\\s \u76f8\u53cd&#xff0c;\u5339\u914d\u4efb\u4f55\u975e\u7a7a\u767d\u5b57\u7b26&#xff1b;\u76f8\u5f53\u4e8e\u7c7b \\\\t\\\\n\\\\r\\\\f\\\\v<\/td>\n<\/tr>\n<tr>\n<td>\\\\w<\/td>\n<td>\u5339\u914d\u4efb\u610f\u4e00\u4e2a\u6587\u5b57\u5b57\u7b26&#xff0c;\u5305\u62ec\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf&#xff0c;\u7b49\u4ef7\u4e8e\u8868\u8fbe\u5f0f[a-zA-Z0-9_]<\/td>\n<\/tr>\n<tr>\n<td>\\\\W<\/td>\n<td>\u4e8e \\\\w \u76f8\u53cd (\u6ce8&#xff1a;re.ASCII \u6807\u5fd7\u4f7f\u5f97 \\\\w \u53ea\u80fd\u5339\u914d ASCII \u5b57\u7b26)<\/td>\n<\/tr>\n<tr>\n<td>\\\\b<\/td>\n<td>\u5339\u914d\u5355\u8bcd\u7684\u5f00\u59cb\u6216\u7ed3\u675f<\/td>\n<\/tr>\n<tr>\n<td>\\\\B<\/td>\n<td>\u4e0e \\\\b \u76f8\u53cd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\n<p>\\\\w \u5339\u914d\u5b57\u6bcd\u6570\u5b57\u4e0b\u5212\u7ebf(\u6c49\u5b57)<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;\u534e\u6e05_\u8fdc\u89c1abc \u534e\u6e05hqyj\u8fdc\u89c1 \u534e\u6e05\u725b\u76ae\u8fdc\u89c1&#034;<br \/>\ndata &#061; re.findall(r&#034;\u534e\u6e05\\\\w&#043;\u8fdc\u89c1&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;\u534e\u6e05_\u8fdc\u89c1&#039;, &#039;\u534e\u6e05hqyj\u8fdc\u89c1&#039;, &#039;\u534e\u6e05\u725b\u76ae\u8fdc\u89c1&#039;] <\/p>\n<ul>\n<li>\n<p>\\\\d\u5339\u914d\u6570\u5b57<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;hqyj66d6 a1h43d3fd43s43d4 &#034;<br \/>\ndata &#061; re.findall(r&#034;d\\\\d&#034;, text) \u00a0# \u53ea\u5339\u914d\u4e00\u4e2a\u6570\u5b57<br \/>\nprint(data) \u00a0# [&#039;d6&#039;, &#039;d3&#039;, &#039;d4&#039;, &#039;d4&#039;]<br \/>\nimport re<br \/>\n\u200b<br \/>\ntext &#061; &#034;hqyj66d6 a1h43d3fd43s43d4 &#034;<br \/>\ndata &#061; re.findall(r&#034;d\\\\d&#043;&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;d6&#039;, &#039;d3&#039;, &#039;d43&#039;, &#039;d4&#039;] <\/p>\n<ul>\n<li>\n<p>\\\\s\u5339\u914d\u4efb\u610f\u7a7a\u767d\u7b26 \u5305\u62ec\u7a7a\u683c,\u5236\u8868\u7b26\u7b49\u7b49<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;hqyj666  jack karen 666&#034;<br \/>\ndata &#061; re.findall(r&#034;\\\\sj\\\\w&#043;\\\\s&#034;, text)<br \/>\nprint(data) \u00a0# [&#039; jack &#039;] <\/p>\n<h3>2. \u6570\u91cf\u63a7\u5236<\/h3>\n<p>\u63a7\u5236\u5339\u914d\u89c4\u5219\u7684\u91cd\u590d\u6b21\u6570~<\/p>\n<h4>2.1 *\u91cd\u590d0\u6b21\u6216\u591a\u6b21<\/h4>\n<p>import re<br \/>\ntext&#061;&#034;\u534e\u6e05\u8fdc\u89c1 \u534e\u6e05666\u8fdc\u89c1&#034;<br \/>\ndata&#061;re.findall(&#034;\u534e\u6e056*\u8fdc\u89c1&#034;,text)<br \/>\nprint(data)#[&#039;\u534e\u6e05\u8fdc\u89c1&#039;, &#039;\u534e\u6e05666\u8fdc\u89c1&#039;] <\/p>\n<h4>2.2 &#043;\u91cd\u590d1\u6b21\u6216\u591a\u6b21<\/h4>\n<p>import re<br \/>\ntext&#061;&#034;\u534e\u6e05\u8fdc\u89c1 \u534e\u6e05666\u8fdc\u89c1 \u534e\u6e056\u8fdc\u89c1&#034;<br \/>\ndata&#061;re.findall(&#034;\u534e\u6e056&#043;\u8fdc\u89c1&#034;,text)<br \/>\nprint(data)#[&#039;\u534e\u6e05666\u8fdc\u89c1&#039;, &#039;\u534e\u6e056\u8fdc\u89c1&#039;] <\/p>\n<h4>2.3 ?\u91cd\u590d1\u6b21\u62160\u6b21<\/h4>\n<p>import re<br \/>\ntext&#061;&#034;\u534e\u6e05\u8fdc\u89c1 \u534e\u6e05666\u8fdc\u89c1 \u534e\u6e056\u8fdc\u89c1&#034;<br \/>\ndata&#061;re.findall(&#034;\u534e\u6e056?\u8fdc\u89c1&#034;,text)<br \/>\nprint(data)#[&#039;\u534e\u6e05\u8fdc\u89c1&#039;, &#039;\u534e\u6e056\u8fdc\u89c1&#039;] <\/p>\n<h4>2.4 {n}\u91cd\u590dn\u6b21<\/h4>\n<p>import re<br \/>\ntext&#061;&#034;\u534e\u6e05\u8fdc\u89c1 \u534e\u6e05666\u8fdc\u89c1 \u534e\u6e056\u8fdc\u89c1&#034;<br \/>\ndata&#061;re.findall(&#034;\u534e\u6e056{3}\u8fdc\u89c1&#034;,text)<br \/>\nprint(data)#[&#039;\u534e\u6e05666\u8fdc\u89c1&#039;] <\/p>\n<h4>2.5 {n,}\u91cd\u590dn\u6b21\u6216\u591a\u6b21<\/h4>\n<p>import re<br \/>\ntext&#061;&#034;\u534e\u6e05\u8fdc\u89c1 \u534e\u6e05666\u8fdc\u89c1 \u534e\u6e056\u8fdc\u89c1 \u534e\u6e0566\u8fdc\u89c1&#034;<br \/>\ndata&#061;re.findall(&#034;\u534e\u6e056{2,}\u8fdc\u89c1&#034;,text)<br \/>\nprint(data)#[&#039;\u534e\u6e05666\u8fdc\u89c1&#039;, &#039;\u534e\u6e0566\u8fdc\u89c1&#039;] <\/p>\n<h4>2.6 {n,m}\u91cd\u590dn\u5230m\u6b21<\/h4>\n<p>import re<br \/>\ntext&#061;&#034;\u534e\u6e05\u8fdc\u89c1 \u534e\u6e05666\u8fdc\u89c1 \u534e\u6e056\u8fdc\u89c1 \u534e\u6e0566\u8fdc\u89c1&#034;<br \/>\ndata&#061;re.findall(&#034;\u534e\u6e056{0,2}\u8fdc\u89c1&#034;,text)<br \/>\nprint(data)#[&#039;\u534e\u6e05\u8fdc\u89c1&#039;, &#039;\u534e\u6e056\u8fdc\u89c1&#039;, &#039;\u534e\u6e0566\u8fdc\u89c1&#039;] <\/p>\n<h3>3. \u5206\u7ec4<\/h3>\n<ul>\n<li>\n<p>()\u63d0\u53d6\u5174\u8da3\u533a\u57df<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;\u8c22\u5e1d\u8c22\u5e1d,\u6211\u8981\u8fea\u58eb\u5c3c,\u6211\u7684\u7535\u8bdd\u53f7\u780118282832341,qq\u53f7\u78011817696843&#034;<br \/>\ndata &#061; re.findall(r&#034;\u53f7\u7801(\\\\d{10,})&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;18282832341&#039;, &#039;1817696843&#039;]<br \/>\nimport re<br \/>\n\u200b<br \/>\ntext &#061; &#034;\u8c22\u5e1d\u8c22\u5e1d,\u6211\u8981\u8fea\u58eb\u5c3c,\u6211\u7684\u7535\u8bdd\u53f7\u780118282832341,qq\u53f7\u78011817696843&#034;<br \/>\ndata &#061; re.findall(r&#034;(\\\\w{2}\u53f7\u7801(\\\\d{10,}))&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;18282832341&#039;, &#039;1817696843&#039;] <\/p>\n<ul>\n<li>\n<p>(|)\u63d0\u53d6\u5174\u8da3\u533a\u57df(| &#061; or)<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;\u7b2c\u4e00\u540d\u5f20\u4e09 \u7b2c\u4e00\u540d\u7269\u7406149\u5206 \u7b2c\u4e00\u540d\u6570\u5b66150\u5206 \u7b2c\u4e00\u540d\u82f1\u8bed148\u5206 \u7b2c\u4e00\u540d\u603b\u5206740\u5206&#034;<br \/>\ndata &#061; re.findall(r&#034;\u7b2c\u4e00\u540d(\\\\w{2,}|\\\\w{2,}\\\\d{2,}\u5206)&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;\u5f20\u4e09&#039;, &#039;\u7269\u7406149\u5206&#039;, &#039;\u6570\u5b66150\u5206&#039;, &#039;\u82f1\u8bed148\u5206&#039;, &#039;\u603b\u5206740\u5206&#039;] <\/p>\n<h3>4. \u5f00\u59cb\u548c\u7ed3\u675f<\/h3>\n<ul>\n<li>\n<p>^\u5f00\u59cb<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\ntext &#061; &#034;hqyj66abc hqyj123&#034;<br \/>\ndata &#061; re.findall(r&#034;^hqyj\\\\d&#043;&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;hqyj66&#039;] <\/p>\n<ul>\n<li>\n<p>$\u7ed3\u5c3e<\/p>\n<\/li>\n<\/ul>\n<p>import re<br \/>\ntext &#061; &#034;hqyj66abc hqyj123&#034;<br \/>\ndata &#061; re.findall(r&#034;hqyj\\\\d&#043;$&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;hqyj123&#039;] <\/p>\n<h3>5. \u7279\u6b8a\u5b57\u7b26<\/h3>\n<p>\u7531\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d* . \\\\ {} () \u7b49\u7b49\u7b26\u53f7\u5177\u6709\u7279\u6b8a\u542b\u4e49,\u5982\u679c\u4f60\u6307\u5b9a\u7684\u5b57\u7b26\u6b63\u597d\u5c31\u662f\u8fd9\u4e9b\u7b26\u53f7,\u9700\u8981\u7528\\\\\u8fdb\u884c\u8f6c\u4e49<\/p>\n<p>import re<br \/>\ntext &#061; &#034;\u6570\u5b66\u4e2d\u96c6\u5408\u7684\u5199\u6cd5\u662f{2}&#034;<br \/>\ndata &#061; re.findall(r&#034;\\\\{2\\\\}&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;{2}&#039;] <\/p>\n<h3>6. \u5e38\u7528\u65b9\u6cd5<\/h3>\n<h4>6.1 re.findall<\/h4>\n<p>\u83b7\u53d6\u5339\u914d\u5230\u7684\u6240\u6709\u6570\u636e<\/p>\n<p>import re<br \/>\ntext&#061;&#034;hqyj66d6 a1h43d3fd43s43d4 &#034;<br \/>\ndata&#061;re.findall(&#034;d\\\\d&#043;&#034;,text)<br \/>\nprint(data)#[&#039;d6&#039;, &#039;d3&#039;, &#039;d43&#039;, &#039;d4&#039;] <\/p>\n<h4>6.2 re.match<\/h4>\n<p>\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d&#xff0c;\u6210\u529f\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u5426\u5219\u8fd4\u56denone\u3002<\/p>\n<p>\u5339\u914d\u6210\u529f\u8fd4\u56de\u5bf9\u8c61,\u5bf9\u8c61\u7684\u65b9\u6cd5:<\/p>\n<table>\n<tr>\u65b9\u6cd5\u529f\u80fd<\/tr>\n<tbody>\n<tr>\n<td>group()<\/td>\n<td>\u8fd4\u56de\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>start()<\/td>\n<td>\u8fd4\u56de\u5339\u914d\u7684\u5f00\u59cb\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>end()<\/td>\n<td>\u8fd4\u56de\u5339\u914d\u7684\u7ed3\u675f\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>span()<\/td>\n<td>\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u8868\u793a\u5339\u914d\u4f4d\u7f6e&#xff08;\u5f00\u59cb&#xff0c;\u7ed3\u675f&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>import re<br \/>\n\u200b<br \/>\n# \u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d&#xff0c;\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5339\u914d (\u5f00\u59cb,\u7ed3\u675f) \u7684\u4f4d\u7f6e\u7684\u5143\u7ec4<br \/>\nprint(re.match(&#039;www&#039;, &#034;www.python.com&#034;).span()) \u00a0 \u00a0 # (0, 3)<br \/>\nprint(re.match(&#039;www&#039;, &#034;www.python.com&#034;).start()) \u00a0 \u00a0# 0<br \/>\nprint(re.match(&#039;www&#039;, &#034;www.python.com&#034;).end()) \u00a0 \u00a0 \u00a0# 3<br \/>\n# \u4e0d\u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d<br \/>\nprint(re.match(&#039;com&#039;, &#034;www.python.com&#034;)) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0# None <\/p>\n<h4>6.3 re.search<\/h4>\n<p>\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6210\u529f\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002\u6210\u529f\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u5426\u5219\u8fd4\u56denone<\/p>\n<p>import re<br \/>\n\u200b<br \/>\n# \u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d<br \/>\nprint(re.search(&#039;www&#039;, &#039;www.hqyj.com&#039;).span()) \u00a0# (0, 3)<br \/>\n# \u4e0d\u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d<br \/>\nprint(re.search(&#039;com&#039;, &#039;www.hqyj.com&#039;).span()) \u00a0# (9, 12) <\/p>\n<h4>6.4 re.sub<\/h4>\n<p>\u66ff\u6362\u5339\u914d\u6210\u529f\u7684\u5b57\u7b26<\/p>\n<p>\u7c7b\u4f3c\u4e0e\u5b57\u7b26\u4e32\u7684replace\u51fd\u6570<\/p>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;\u4ee5\u524d\u534e\u6e05\u8fdc\u89c1\u5728\u56db\u5ddd\u5927\u5b66\u65c1\u8fb9,\u73b0\u5728\u534e\u6e05\u8fdc\u89c1\u5728\u897f\u5357\u4ea4\u5927\u65c1\u8fb9&#034;<br \/>\ndata &#061; re.sub(&#034;\u534e\u6e05\u8fdc\u89c1&#034;, &#034;\u5317\u4eac\u534e\u6e05\u8fdc\u89c1\u79d1\u6280\u96c6\u56e2\u6210\u90fd\u4e2d\u5fc3&#034;, text)<br \/>\nprint(data) \u00a0# \u4ee5\u524d\u5317\u4eac\u534e\u6e05\u8fdc\u89c1\u79d1\u6280\u96c6\u56e2\u6210\u90fd\u4e2d\u5fc3\u5728\u56db\u5ddd\u5927\u5b66\u65c1\u8fb9,\u73b0\u5728\u5317\u4eac\u534e\u6e05\u8fdc\u89c1\u79d1\u6280\u96c6\u56e2\u6210\u90fd\u4e2d\u5fc3\u5728\u897f\u5357\u4ea4\u5927\u65c1\u8fb9 <\/p>\n<h4>6.5 re.split<\/h4>\n<p>\u6839\u636e\u5339\u914d\u6210\u529f\u7684\u4f4d\u7f6e\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272<\/p>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;python is \u00a0 very easy&#034;<br \/>\ndata &#061; re.split(r&#034;\\\\s{1,}&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;python&#039;, &#039;is&#039;, &#039;very&#039;, &#039;easy&#039;] <\/p>\n<h4>dd6.6 re.finditer<\/h4>\n<p>\u7c7b\u4f3cfindall \u4f46\u662f\u4e0d\u4f1a\u5168\u90e8\u8fd4\u56de\u51fa\u6765 \u800c\u662f\u8fd4\u56de\u8fed\u4ee3\u5668(\u6bd4\u5982\u5339\u914d\u6210\u529f\u4e8610\u4e07\u4e2a \u5168\u90e8\u8fd4\u56de\u5c31\u5f88\u5403\u5185\u5b58\u4e86)<\/p>\n<p>import re<br \/>\n\u200b<br \/>\ntext &#061; &#034;python is \u00a0 very easy&#034;<br \/>\ndata &#061; re.findall(r&#034;\\\\w&#043;&#034;, text)<br \/>\nprint(data) \u00a0# [&#039;python&#039;, &#039;is&#039;, &#039;very&#039;, &#039;easy&#039;]<br \/>\nimport re<br \/>\n\u200b<br \/>\ntext &#061; &#034;python is \u00a0 very easy&#034;<br \/>\ndata &#061; re.finditer(r&#034;\\\\w&#043;&#034;, text)<br \/>\nprint(data)<br \/>\nfor el in data:<br \/>\n \u00a0 \u00a0print(el.group())<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb391\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u672c\u6587\u6458\u8981\uff1aPython\u6a21\u5757\u4e0e\u5305\u662f\u4ee3\u7801\u7ec4\u7ec7\u7684\u91cd\u8981\u65b9\u5f0f\uff0c\u6a21\u5757\u662f\u5355\u4e2a.py\u6587\u4ef6\uff0c\u5305\u662f\u5305\u542b__init__.py\u7684\u76ee\u5f55\u3002\u7cfb\u7edf\u6a21\u5757\uff08\u5982math\/os\uff09\u3001\u7b2c\u4e09\u65b9\u6a21\u5757\uff08\u5982numpy\/pandas\uff09\u548c\u81ea\u5b9a\u4e49\u6a21\u5757\u6784\u6210\u4e86Python\u7684\u6a21\u5757\u4f53\u7cfb\u3002\u6587\u4ef6\u64cd\u4f5c\u5305\u62ecopen\/read\/write\/close\u7b49\u57fa\u672c\u65b9\u6cd5\uff0c\u63a8\u8350\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7re\u6a21\u5757\u63d0\u4f9b\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u80fd\u529b\uff0c\u5305\u542b\u5b57\u7b26\u5339\u914d\u3001\u6570\u91cf\u63a7\u5236\u3001\u5206\u7ec4\u7b49\u6838\u5fc3\u529f\u80fd\uff0c\u5e38\u7528\u65b9\u6cd5\u6709findall\u3001match\u3001search\u3001sub\u7b49\u3002\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u6784\u6210\u4e86Python\u7f16\u7a0b\u7684\u57fa\u7840\u5de5<\/p>\n","protected":false},"author":2,"featured_media":49773,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190],"topic":[],"class_list":["post-49775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u5305\u548c\u6a21\u5757Day8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/49775.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u5305\u548c\u6a21\u5757Day8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb391\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u672c\u6587\u6458\u8981\uff1aPython\u6a21\u5757\u4e0e\u5305\u662f\u4ee3\u7801\u7ec4\u7ec7\u7684\u91cd\u8981\u65b9\u5f0f\uff0c\u6a21\u5757\u662f\u5355\u4e2a.py\u6587\u4ef6\uff0c\u5305\u662f\u5305\u542b__init__.py\u7684\u76ee\u5f55\u3002\u7cfb\u7edf\u6a21\u5757\uff08\u5982math\/os\uff09\u3001\u7b2c\u4e09\u65b9\u6a21\u5757\uff08\u5982numpy\/pandas\uff09\u548c\u81ea\u5b9a\u4e49\u6a21\u5757\u6784\u6210\u4e86Python\u7684\u6a21\u5757\u4f53\u7cfb\u3002\u6587\u4ef6\u64cd\u4f5c\u5305\u62ecopen\/read\/write\/close\u7b49\u57fa\u672c\u65b9\u6cd5\uff0c\u63a8\u8350\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7re\u6a21\u5757\u63d0\u4f9b\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u80fd\u529b\uff0c\u5305\u542b\u5b57\u7b26\u5339\u914d\u3001\u6570\u91cf\u63a7\u5236\u3001\u5206\u7ec4\u7b49\u6838\u5fc3\u529f\u80fd\uff0c\u5e38\u7528\u65b9\u6cd5\u6709findall\u3001match\u3001search\u3001sub\u7b49\u3002\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u6784\u6210\u4e86Python\u7f16\u7a0b\u7684\u57fa\u7840\u5de5\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/49775.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T11:36:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730113655-688a03d74a6a5.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49775.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/49775.html\",\"name\":\"Python\u5305\u548c\u6a21\u5757Day8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T11:36:58+00:00\",\"dateModified\":\"2025-07-30T11:36:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/49775.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/49775.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49775.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u5305\u548c\u6a21\u5757Day8\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u5305\u548c\u6a21\u5757Day8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/49775.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u5305\u548c\u6a21\u5757Day8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb391\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u672c\u6587\u6458\u8981\uff1aPython\u6a21\u5757\u4e0e\u5305\u662f\u4ee3\u7801\u7ec4\u7ec7\u7684\u91cd\u8981\u65b9\u5f0f\uff0c\u6a21\u5757\u662f\u5355\u4e2a.py\u6587\u4ef6\uff0c\u5305\u662f\u5305\u542b__init__.py\u7684\u76ee\u5f55\u3002\u7cfb\u7edf\u6a21\u5757\uff08\u5982math\/os\uff09\u3001\u7b2c\u4e09\u65b9\u6a21\u5757\uff08\u5982numpy\/pandas\uff09\u548c\u81ea\u5b9a\u4e49\u6a21\u5757\u6784\u6210\u4e86Python\u7684\u6a21\u5757\u4f53\u7cfb\u3002\u6587\u4ef6\u64cd\u4f5c\u5305\u62ecopen\/read\/write\/close\u7b49\u57fa\u672c\u65b9\u6cd5\uff0c\u63a8\u8350\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7re\u6a21\u5757\u63d0\u4f9b\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u80fd\u529b\uff0c\u5305\u542b\u5b57\u7b26\u5339\u914d\u3001\u6570\u91cf\u63a7\u5236\u3001\u5206\u7ec4\u7b49\u6838\u5fc3\u529f\u80fd\uff0c\u5e38\u7528\u65b9\u6cd5\u6709findall\u3001match\u3001search\u3001sub\u7b49\u3002\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u6784\u6210\u4e86Python\u7f16\u7a0b\u7684\u57fa\u7840\u5de5","og_url":"https:\/\/www.wsisp.com\/helps\/49775.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T11:36:58+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730113655-688a03d74a6a5.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/49775.html","url":"https:\/\/www.wsisp.com\/helps\/49775.html","name":"Python\u5305\u548c\u6a21\u5757Day8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T11:36:58+00:00","dateModified":"2025-07-30T11:36:58+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/49775.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/49775.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/49775.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u5305\u548c\u6a21\u5757Day8"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=49775"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49775\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/49773"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=49775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=49775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=49775"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=49775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}