{"id":926899,"date":"2024-12-26T15:58:26","date_gmt":"2024-12-26T07:58:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/926899.html"},"modified":"2024-12-26T15:58:28","modified_gmt":"2024-12-26T07:58:28","slug":"python%e5%a6%82%e4%bd%95%e6%89%b9%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/926899.html","title":{"rendered":"python\u5982\u4f55\u6279\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063245\/134c188b-9762-41d0-a5d2-394e4a4d6212.webp\" alt=\"python\u5982\u4f55\u6279\u91cf\" \/><\/p>\n<p><h3>PYTHON\u6279\u91cf\u5904\u7406\u7684\u6838\u5fc3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u6279\u91cf\u5904\u7406\u4e3b\u8981\u4f9d\u8d56\u4e8e<strong>\u5217\u8868\u89e3\u6790\u3001\u5faa\u73af\u8bed\u53e5\u3001\u6a21\u5757\u5316\u7f16\u7a0b<\/strong>\u3002\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u5217\u8868\uff0c\u5faa\u73af\u8bed\u53e5\u5219\u7528\u4e8e\u8fed\u4ee3\u5904\u7406\u591a\u4e2a\u6570\u636e\uff0c\u6a21\u5757\u5316\u7f16\u7a0b\u5219\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<strong>\u5217\u8868\u89e3\u6790<\/strong>\u53ef\u4ee5\u901a\u8fc7\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u5bf9\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6279\u91cf\u64cd\u4f5c\uff0c<strong>\u5faa\u73af\u8bed\u53e5<\/strong>\u5219\u662f\u901a\u8fc7for\u5faa\u73af\u6216while\u5faa\u73af\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u6279\u91cf\u5904\u7406\uff0c<strong>\u6a21\u5757\u5316\u7f16\u7a0b<\/strong>\u5219\u662f\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u6216\u7c7b\u6765\u5b9e\u73b0\u6279\u91cf\u5904\u7406\u7684\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5b9e\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u73b0\u6709\u5217\u8868\u8fdb\u884c\u6279\u91cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u8bed\u6cd5<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<code>[expression for item in iterable]<\/code>\u3002\u5176\u4e2d\uff0c<code>expression<\/code>\u662f\u5bf9\u6bcf\u4e2a<code>item<\/code>\u8fdb\u884c\u5904\u7406\u7684\u8868\u8fbe\u5f0f\uff0c<code>iterable<\/code>\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5b9e\u4f8b\u8bb2\u89e3<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\uff0c\u6211\u4eec\u5e0c\u671b\u5bf9\u6bcf\u4e2a\u6570\u5b57\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared_numbers = [x2 for x in numbers]<\/p>\n<p>print(squared_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>squared_numbers<\/code>\u5c31\u662f\u901a\u8fc7\u5217\u8868\u89e3\u6790\u751f\u6210\u7684\u65b0\u5217\u8868\uff0c\u5305\u542b\u4e86<code>numbers<\/code>\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5faa\u73af\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u8bed\u53e5\u7528\u4e8e\u8fed\u4ee3\u5904\u7406\u591a\u4e2a\u6570\u636e\u9879\uff0c\u5e38\u89c1\u7684\u5faa\u73af\u8bed\u53e5\u5305\u62ec<code>for<\/code>\u548c<code>while<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2.1 for\u5faa\u73af<\/h4>\n<\/p>\n<p><p><code>for<\/code>\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\uff0c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u6307\u5b9a\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u904d\u5386\u4e86<code>fruits<\/code>\u5217\u8868\uff0c\u5e76\u6253\u5370\u6bcf\u4e2a\u6c34\u679c\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>2.2 while\u5faa\u73af<\/h4>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u5219\u662f\u57fa\u4e8e\u6761\u4ef6\u8fdb\u884c\u8fed\u4ee3\uff0c\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u4f1a\u91cd\u590d\u6267\u884c\u5faa\u73af\u4f53\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(&quot;Hello, world!&quot;)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u4f1a\u6253\u53705\u6b21&quot;Hello, world!&quot;\uff0c\u56e0\u4e3a<code>count<\/code>\u4ece0\u5f00\u59cb\uff0c\u6bcf\u6b21\u5faa\u73af\u540e\u589e\u52a01\uff0c\u76f4\u5230<code>count<\/code>\u4e0d\u5c0f\u4e8e5\u65f6\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6a21\u5757\u5316\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u5316\u7f16\u7a0b\u901a\u8fc7\u5c06\u4ee3\u7801\u5212\u5206\u4e3a\u72ec\u7acb\u7684\u6a21\u5757\uff08\u51fd\u6570\u3001\u7c7b\uff09\u6765\u5b9e\u73b0\u91cd\u590d\u4f7f\u7528\u548c\u66f4\u597d\u7684\u7ed3\u6784\u5316\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u662f\u6a21\u5757\u5316\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u5c06\u91cd\u590d\u7684\u4ee3\u7801\u5c01\u88c5\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>names = [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;]<\/p>\n<p>for name in names:<\/p>\n<p>    greet(name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570<code>greet<\/code>\u5c01\u88c5\u4e86\u6253\u5370\u95ee\u5019\u8bed\u7684\u529f\u80fd\uff0c\u901a\u8fc7\u5faa\u73af\u904d\u5386<code>names<\/code>\u5217\u8868\uff0c\u6279\u91cf\u8c03\u7528<code>greet<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u7c7b<\/h4>\n<\/p>\n<p><p>\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u6570\u636e\u548c\u529f\u80fd\u5c01\u88c5\u5728\u4e00\u8d77\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6279\u91cf\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def speak(self):<\/p>\n<p>        print(f&quot;{self.name} makes a sound.&quot;)<\/p>\n<p>animals = [Animal(&quot;Dog&quot;), Animal(&quot;Cat&quot;), Animal(&quot;Cow&quot;)]<\/p>\n<p>for animal in animals:<\/p>\n<p>    animal.speak()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7b<code>Animal<\/code>\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>__init__<\/code>\u65b9\u6cd5\u548c\u4e00\u4e2a<code>speak<\/code>\u65b9\u6cd5\uff0c\u901a\u8fc7\u521b\u5efa<code>Animal<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u6279\u91cf\u8c03\u7528<code>speak<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6279\u91cf\u5904\u7406\u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u8bfb\u53d6\u591a\u4e2a\u6587\u4ef6\u7684\u6570\u636e\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528os\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u529f\u80fd\u7684\u8bbf\u95ee\uff0c\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory = &quot;\/path\/to\/directory&quot;<\/p>\n<p>for filename in os.listdir(directory):<\/p>\n<p>    if filename.endswith(&quot;.txt&quot;):<\/p>\n<p>        with open(os.path.join(directory, filename)) as f:<\/p>\n<p>            content = f.read()<\/p>\n<p>            print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u4e86\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709<code>.txt<\/code>\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528glob\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>glob<\/code>\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u6587\u4ef6\u540d\u6a21\u5f0f\u5339\u914d\u529f\u80fd\uff0c\u53ef\u4ee5\u6279\u91cf\u5904\u7406\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<p>for filepath in glob.glob(&quot;\/path\/to\/directory\/*.txt&quot;):<\/p>\n<p>    with open(filepath) as f:<\/p>\n<p>        content = f.read()<\/p>\n<p>        print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u4e86<code>glob<\/code>\u6a21\u5757\u7684<code>glob<\/code>\u51fd\u6570\uff0c\u6279\u91cf\u5904\u7406\u76ee\u5f55\u4e2d\u6240\u6709<code>.txt<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6279\u91cf\u5904\u7406\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u7684\u6279\u91cf\u5904\u7406\u5728\u6570\u636e\u5206\u6790\u3001\u6570\u636e\u6e05\u6d17\u7b49\u9886\u57df\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528pandas\u5e93<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u6279\u91cf\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>data[&#39;new_column&#39;] = data[&#39;old_column&#39;].apply(lambda x: x*2)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6\u4e86\u4e00\u4e2aCSV\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>apply<\/code>\u65b9\u6cd5\u5bf9\u4e00\u5217\u6570\u636e\u8fdb\u884c\u6279\u91cf\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528numpy\u5e93<\/h4>\n<\/p>\n<p><p><code>numpy<\/code>\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u5408\u4e8e\u6570\u503c\u6570\u636e\u7684\u6279\u91cf\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>squared_array = np.square(array)<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>numpy<\/code>\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6279\u91cf\u7f51\u7edc\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u6279\u91cf\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u4e5f\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u4f7f\u7528requests\u5e93<\/h4>\n<\/p>\n<p><p><code>requests<\/code>\u5e93\u662fPython\u4e2d\u6700\u6d41\u884c\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u53d1\u9001\u548c\u5904\u7406HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>urls = [&quot;http:\/\/example.com&quot;, &quot;http:\/\/example.org&quot;]<\/p>\n<p>for url in urls:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    print(response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6279\u91cf\u53d1\u9001HTTP GET\u8bf7\u6c42\uff0c\u5e76\u6253\u5370\u6bcf\u4e2a\u8bf7\u6c42\u7684\u72b6\u6001\u7801\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u4f7f\u7528\u591a\u7ebf\u7a0b\u63d0\u9ad8\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u91cf\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    print(f&quot;URL: {url}, Status Code: {response.status_code}&quot;)<\/p>\n<p>threads = []<\/p>\n<p>for url in urls:<\/p>\n<p>    thread = threading.Thread(target=fetch_url, args=(url,))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u521b\u5efa\u4e86\u591a\u4e2a\u7ebf\u7a0b\u6765\u5e76\u53d1\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6279\u91cf\u5904\u7406\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u5904\u7406\u662f\u53e6\u4e00\u4e2a\u9700\u8981\u6279\u91cf\u5904\u7406\u7684\u5e38\u89c1\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u4f7f\u7528PIL\u5e93<\/h4>\n<\/p>\n<p><p>PIL\uff08Pillow\uff09\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u6279\u91cf\u5904\u7406\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import os<\/p>\n<p>directory = &quot;\/path\/to\/images&quot;<\/p>\n<p>for filename in os.listdir(directory):<\/p>\n<p>    if filename.endswith(&quot;.jpg&quot;):<\/p>\n<p>        with Image.open(os.path.join(directory, filename)) as img:<\/p>\n<p>            img = img.convert(&quot;L&quot;)  # \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>            img.save(os.path.join(directory, f&quot;gray_{filename}&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528PIL\u5e93\u5c06\u76ee\u5f55\u4e2d\u6240\u6709JPG\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>7.2 \u4f7f\u7528OpenCV\u5e93<\/h4>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6279\u91cf\u5904\u7406\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import glob<\/p>\n<p>for filepath in glob.glob(&quot;\/path\/to\/images\/*.jpg&quot;):<\/p>\n<p>    img = cv2.imread(filepath)<\/p>\n<p>    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    cv2.imwrite(filepath.replace(&quot;.jpg&quot;, &quot;_gray.jpg&quot;), gray)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528OpenCV\u5c06\u6bcf\u5f20\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff0c\u5e76\u4fdd\u5b58\u65b0\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u4e2a\u7ef4\u5ea6\u7684\u4ecb\u7ecd\uff0c\u4f60\u73b0\u5728\u5e94\u8be5\u5bf9Python\u5982\u4f55\u8fdb\u884c\u6279\u91cf\u5904\u7406\u6709\u4e86\u5168\u9762\u7684\u4e86\u89e3\u3002\u8fd9\u4e9b\u6280\u672f\u548c\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5404\u79cd\u5e94\u7528\u573a\u666f\u4e2d\u5b9e\u73b0\u9ad8\u6548\u7684\u6279\u91cf\u6570\u636e\u3001\u6587\u4ef6\u3001\u7f51\u7edc\u8bf7\u6c42\u548c\u56fe\u50cf\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u53ef\u4ee5\u7528\u4e8e\u54ea\u4e9b\u6279\u91cf\u5904\u7406\u4efb\u52a1\uff1f<\/strong><br \/>Python\u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u6279\u91cf\u5904\u7406\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u6279\u91cf\u5904\u7406\u6587\u4ef6\uff08\u5982\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u3001\u56fe\u50cf\u7b49\uff09\uff0c\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u4e0e\u8f6c\u6362\uff0c\u81ea\u52a8\u5316\u7f51\u7edc\u722c\u866b\u4ee5\u6293\u53d6\u5927\u91cf\u6570\u636e\uff0c\u6216\u662f\u6267\u884c\u6279\u91cf\u8ba1\u7b97\u4e0e\u5206\u6790\u3002\u8fd9\u4e9b\u4efb\u52a1\u53ef\u4ee5\u901a\u8fc7Python\u7684\u4e30\u5bcc\u5e93\uff08\u5982Pandas\u3001NumPy\u3001BeautifulSoup\u7b49\uff09\u6765\u5b9e\u73b0\uff0c\u63d0\u9ad8\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u6279\u91cf\u6587\u4ef6\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>os<\/code>\u548c<code>shutil<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u6279\u91cf\u6587\u4ef6\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408\u5faa\u73af\u6765\u5bf9\u6bcf\u4e2a\u6587\u4ef6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\uff08\u5982\u91cd\u547d\u540d\u3001\u79fb\u52a8\u6216\u590d\u5236\uff09\u3002\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u590d\u5236\u4e0e\u5220\u9664\uff0c\u7b80\u5316\u4e86\u6279\u91cf\u64cd\u4f5c\u7684\u4ee3\u7801\u590d\u6742\u6027\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684Python\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u6279\u91cf\u6570\u636e\u5904\u7406\uff1f<\/strong><br \/>\u5bf9\u4e8e\u6279\u91cf\u6570\u636e\u5904\u7406\uff0cPandas\u662f\u6700\u53d7\u6b22\u8fce\u7684\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002NumPy\u5219\u4e13\u6ce8\u4e8e\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u7ec4\u548c\u77e9\u9635\u3002\u5bf9\u4e8e\u7f51\u7edc\u6570\u636e\u6293\u53d6\uff0cBeautifulSoup\u548cScrapy\u53ef\u4ee5\u5e2e\u52a9\u60a8\u9ad8\u6548\u5730\u63d0\u53d6\u7f51\u9875\u4fe1\u606f\u3002\u7ed3\u5408\u8fd9\u4e9b\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6279\u91cf\u6570\u636e\u5904\u7406\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON\u6279\u91cf\u5904\u7406\u7684\u6838\u5fc3\u65b9\u6cd5 \u5728Python\u4e2d\u8fdb\u884c\u6279\u91cf\u5904\u7406\u4e3b\u8981\u4f9d\u8d56\u4e8e\u5217\u8868\u89e3\u6790\u3001\u5faa\u73af\u8bed\u53e5\u3001\u6a21\u5757\u5316\u7f16\u7a0b\u3002\u5217\u8868\u89e3\u6790 [&hellip;]","protected":false},"author":3,"featured_media":926900,"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\/926899"}],"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=926899"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/926899\/revisions"}],"predecessor-version":[{"id":926901,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/926899\/revisions\/926901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/926900"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=926899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=926899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=926899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}