{"id":962638,"date":"2024-12-27T04:10:10","date_gmt":"2024-12-26T20:10:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962638.html"},"modified":"2024-12-27T04:10:11","modified_gmt":"2024-12-26T20:10:11","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962638.html","title":{"rendered":"python\u5982\u4f55\u5199\u8f6c\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104535\/78dcb63a-0f1c-46bb-9761-06fca5ffb3f3.webp\" alt=\"python\u5982\u4f55\u5199\u8f6c\u6362\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8fdb\u884c\u8f6c\u6362\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u590d\u6742\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u5229\u7528\u5e93\u8fdb\u884c\u683c\u5f0f\u8f6c\u6362\u3002<\/strong> \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u8f6c\u6362\u65b9\u6cd5\u4e4b\u4e00\u3002Python\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5185\u7f6e\u51fd\u6570\u5982<code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>str()<\/code>\u7b49\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u6d6e\u70b9\u6570\uff0c\u6216\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u9700\u6c42\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u4e0d\u540c\u7c7b\u578b\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u662f\u6211\u4eec\u5728\u7f16\u7a0b\u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u6574\u6570\u548c\u6d6e\u70b9\u6570\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6574\u6570\u548c\u6d6e\u70b9\u6570\u4e4b\u95f4\u7684\u8f6c\u6362\u662f\u975e\u5e38\u76f4\u89c2\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u8fd9\u5c06\u622a\u6389\u5c0f\u6570\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u56db\u820d\u4e94\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_float = 3.14<\/p>\n<p>num_int = int(num_float)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u53cd\uff0c\u4f7f\u7528<code>float()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_int = 5<\/p>\n<p>num_float = float(num_int)<\/p>\n<p>print(num_float)  # \u8f93\u51fa: 5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u5b57\u7b26\u4e32\u548c\u6570\u5b57\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u548c<code>float()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p>num_str_float = &quot;123.45&quot;<\/p>\n<p>num_float = float(num_str_float)<\/p>\n<p>print(num_float)  # \u8f93\u51fa: 123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 456<\/p>\n<p>num_str = str(num)<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;456&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3 \u5e03\u5c14\u503c\u548c\u6570\u5b57\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5e03\u5c14\u503c\u5728Python\u4e2d\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u5176\u4e2d<code>True<\/code>\u7b49\u4ef7\u4e8e1\uff0c<code>False<\/code>\u7b49\u4ef7\u4e8e0\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bool_val = True<\/p>\n<p>num = int(bool_val)<\/p>\n<p>print(num)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53cd\u8fc7\u6765\uff0c\u6570\u5b57\u4e5f\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u975e\u96f6\u6570\u5b57\u8f6c\u6362\u4e3a<code>True<\/code>\uff0c\u96f6\u8f6c\u6362\u4e3a<code>False<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 0<\/p>\n<p>bool_val = bool(num)<\/p>\n<p>print(bool_val)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u590d\u6742\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff0c\u7279\u522b\u662f\u6d89\u53ca\u5230\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u573a\u666f\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7f16\u5199\u81ea\u5df1\u7684\u8f6c\u6362\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u81ea\u5b9a\u4e49\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b<code>Person<\/code>\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5c06\u5176\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49<code>__str__<\/code>\u6216<code>__repr__<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return f&quot;Person(name={self.name}, age={self.age})&quot;<\/p>\n<p>person = Person(&quot;Alice&quot;, 30)<\/p>\n<p>print(person)  # \u8f93\u51fa: Person(name=Alice, age=30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u89e3\u6790\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_person(s):<\/p>\n<p>    name, age = s.split(&#39;,&#39;)<\/p>\n<p>    return Person(name.strip(), int(age.strip()))<\/p>\n<p>person_str = &quot;Bob, 25&quot;<\/p>\n<p>person = parse_person(person_str)<\/p>\n<p>print(person)  # \u8f93\u51fa: Person(name=Bob, age=25)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528\u5e93\u8fdb\u884c\u683c\u5f0f\u8f6c\u6362<\/p>\n<\/p>\n<p><p>Python\u7684\u4e30\u5bcc\u751f\u6001\u7cfb\u7edf\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u683c\u5f0f\u8f6c\u6362\u7684\u5e93\uff0c\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u6211\u4eec\u7684\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>3.1 JSON\u683c\u5f0f\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>JSON\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u5e93\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u6216\u8005\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35}<\/p>\n<p>json_str = json.dumps(data)<\/p>\n<p>print(json_str)  # \u8f93\u51fa: &#39;{&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35}&#39;<\/p>\n<p>data_parsed = json.loads(json_str)<\/p>\n<p>print(data_parsed)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 XML\u683c\u5f0f\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eXML\u683c\u5f0f\u7684\u8f6c\u6362\uff0cPython\u63d0\u4f9b\u4e86<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u3002\u8fd9\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684XML\u5904\u7406\u5e93\uff0c\u9002\u5408\u5927\u591a\u6570XML\u8f6c\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<p>xml_data = &#39;&lt;person&gt;&lt;name&gt;David&lt;\/name&gt;&lt;age&gt;40&lt;\/age&gt;&lt;\/person&gt;&#39;<\/p>\n<p>root = ET.fromstring(xml_data)<\/p>\n<p>name = root.find(&#39;name&#39;).text<\/p>\n<p>age = int(root.find(&#39;age&#39;).text)<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)  # \u8f93\u51fa: Name: David, Age: 40<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u6216\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u4e8c\u8fdb\u5236\u6570\u636e\u548c\u5176\u4ed6\u683c\u5f0f\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u5b57\u7b26\u4e32\u548c\u5b57\u8282\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u4e4b\u95f4\u7684\u8f6c\u6362\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u548c<code>decode()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello, World!&quot;<\/p>\n<p>b = s.encode(&#39;utf-8&#39;)<\/p>\n<p>print(b)  # \u8f93\u51fa: b&#39;Hello, World!&#39;<\/p>\n<p>s_decoded = b.decode(&#39;utf-8&#39;)<\/p>\n<p>print(s_decoded)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u7ed3\u6784\u5316\u6570\u636e\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u683c\u5f0f\uff0cPython\u63d0\u4f9b\u4e86<code>struct<\/code>\u6a21\u5757\uff0c\u7528\u4e8e\u5c06Python\u503c\u8f6c\u6362\u4e3aC\u7ed3\u6784\u4f53\u8868\u793a\u5f62\u5f0f\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u5c06\u6574\u6570\u548c\u6d6e\u70b9\u6570\u6253\u5305\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e<\/strong><\/h2>\n<p>data = struct.pack(&#39;i f&#39;, 10, 2.5)<\/p>\n<p>print(data)  # \u8f93\u51fa: \u4e8c\u8fdb\u5236\u6570\u636e<\/p>\n<h2><strong>\u89e3\u5305\u4e8c\u8fdb\u5236\u6570\u636e<\/strong><\/h2>\n<p>unpacked_data = struct.unpack(&#39;i f&#39;, data)<\/p>\n<p>print(unpacked_data)  # \u8f93\u51fa: (10, 2.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u7684\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5904\u7406\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\u90fd\u975e\u5e38\u5173\u952e\u3002Python\u63d0\u4f9b\u4e86<code>datetime<\/code>\u6a21\u5757\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u5b57\u7b26\u4e32\u548c\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>strptime()<\/code>\u548c<code>strftime()<\/code>\u51fd\u6570\u5728\u5b57\u7b26\u4e32\u548c\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/strong><\/h2>\n<p>date_str = &quot;2023-03-15 12:45:00&quot;<\/p>\n<p>date_obj = datetime.strptime(date_str, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(date_obj)  # \u8f93\u51fa: 2023-03-15 12:45:00<\/p>\n<h2><strong>\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str_formatted = date_obj.strftime(&quot;%B %d, %Y&quot;)<\/p>\n<p>print(date_str_formatted)  # \u8f93\u51fa: March 15, 2023<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u65f6\u95f4\u6233\u548c\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u662f\u8ba1\u7b97\u673a\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u57fa\u7840\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u65f6\u95f4\u6233\u548c\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/p>\n<p>timestamp = datetime.now().timestamp()<\/p>\n<p>print(timestamp)<\/p>\n<h2><strong>\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/strong><\/h2>\n<p>date_from_timestamp = datetime.fromtimestamp(timestamp)<\/p>\n<p>print(date_from_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u6587\u672c\u6570\u636e\u7684\u6a21\u5f0f\u5339\u914d\u548c\u8f6c\u6362\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u67e5\u627e\u548c\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u67e5\u627e\u548c\u66ff\u6362\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;The quick brown fox jumps over the lazy dog.&quot;<\/p>\n<h2><strong>\u5c06\u6240\u6709\u5355\u8bcd\u66ff\u6362\u4e3a\u5927\u5199<\/strong><\/h2>\n<p>text_modified = re.sub(r&#39;\\b\\w+\\b&#39;, lambda m: m.group(0).upper(), text)<\/p>\n<p>print(text_modified)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2 \u63d0\u53d6\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;My em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l is example@example.com and my phone number is 123-456-7890.&quot;<\/p>\n<h2><strong>\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/h2>\n<p>email = re.search(r&#39;\\S+@\\S+&#39;, text).group()<\/p>\n<p>print(email)<\/p>\n<h2><strong>\u63d0\u53d6\u7535\u8bdd\u53f7\u7801<\/strong><\/h2>\n<p>phone_number = re.search(r&#39;\\d{3}-\\d{3}-\\d{4}&#39;, text).group()<\/p>\n<p>print(phone_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u8fdb\u884c\u5404\u79cd\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u8f6c\u6362\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6216\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c<code>float()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6216\u6574\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c<code>str()<\/code>\u51fd\u6570\u5219\u7528\u4e8e\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u786e\u4fdd\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u6570\u636e\u7684\u683c\u5f0f\u662f\u6b63\u786e\u7684\uff0c\u4ee5\u907f\u514d\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8f6c\u6362\u5217\u8868\u4e0e\u5143\u7ec4\uff1f<\/strong><br \/>\u8f6c\u6362\u5217\u8868\u4e0e\u5143\u7ec4\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u800c\u4f7f\u7528<code>tuple()<\/code>\u51fd\u6570\u5219\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u8fd9\u79cd\u8f6c\u6362\u5728\u9700\u8981\u4e0d\u540c\u6570\u636e\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u800c\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u4e0e\u8f6c\u6362\uff1f<\/strong><br \/>\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\uff0cf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u6216\u8005\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u6570\u5b57\u3001\u65e5\u671f\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8f93\u51fa\u3002\u4f8b\u5982\uff0cf-string\u53ef\u4ee5\u8ba9\u4f60\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5f15\u7528\u53d8\u91cf\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8fdb\u884c\u8f6c\u6362\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u590d\u6742\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u5229 [&hellip;]","protected":false},"author":3,"featured_media":962641,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962638"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=962638"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962638\/revisions"}],"predecessor-version":[{"id":962642,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962638\/revisions\/962642"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962641"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}