pyhton3 zip() ;sys.stdout;

1、zip()

>>> a =[one,two,three]

>>> b=[1,2,3]

>>> zipped=zip(a,b)

>>> print(zipped)

<zip object at 0x10396f588>

python 3 中,zip() 后返回的是一个可迭代的对象,所以以上例子中返回的是一个对象,而非具体的值。

>>> c = zip(a,b)

>>> for value in c:

...     print(value)

... 

('one', 1)

('two', 2)

('three', 3)

python2 中,直接返回值

>>> zipped=zip(a,b)

>>> print(zipped)

... 

('one', 1)

('two', 2)

('three', 3)

2、sys.stdout

import  sys
f_result=open('result.txt', 'w')
sys.stdout=f_result                # 将print 输出到文本中

3、python2 与python3 判断字典有无某个key的区别

‘’‘python2’‘’
def cmpjson(x, y):
    if x.has_key('name'):
        return x['name'] > y['name']


'''python3'''
def cmpjson(x, y):
    if x.__contains__('name'):
        return x['name'] > y['name']
PS C:\Users\Administrator\Desktop> # PythonEnvRepair_Final_Complete.ps1 PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 1. 动态获取桌面路径 PS C:\Users\Administrator\Desktop> $desktopPath = [Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop) PS C:\Users\Administrator\Desktop> if (-not (Test-Path $desktopPath)) { >> $desktopPath = "C:\Users\Administrator\Desktop_Backup" >> Write-Host "使用备用桌面路径: $desktopPath" -ForegroundColor Yellow >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 2. 设置全局编码 PS C:\Users\Administrator\Desktop> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 PS C:\Users\Administrator\Desktop> $env:PYTHONUTF8 = 1 PS C:\Users\Administrator\Desktop> $env:PYTHONIOENCODING = "utf-8" PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 3. 定义修复函数 PS C:\Users\Administrator\Desktop> function global:Repair-PythonSite { >> param([string]$PythonPath = "E:\Python310") >> >> # 创建site-packages目录(修复变量名) >> $sitePackages = Join-Path $PythonPath "Lib\site-packages" >> if (-not (Test-Path $sitePackages)) { >> New-Item -ItemType Directory -Path $site极客时间Packages -Force | Out-Null >> } >> >> # 创建sitecustomize.py(修复属性设置逻辑) >> $sitecustomizePath = Join-Path $sitePackages "sitecustomize.py" >> $sitecustomizeContent = @" >> import sys >> import os >> import io >> >> # 设置UTF-8编码(兼容Python 3.6及以下) >> try: >> sys.stdout.reconfigure(encoding=&#39;utf-8&#39;) >> sys.stderr.reconfigure(encoding=&#39;utf-8&#39;) >> except AttributeError: >> if sys.version_info < (3, 7): >> sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding=&#39;utf-8&#39;, errors=&#39;replace&#39;, line_buffering=True) >> sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding=&#39;utf-8&#39;, errors=&#39;replace&#39;, line_buffering=True) >> >> # 添加桌面路径到sys.path >> desktop_path = r&#39;$($desktopPath -replace &#39;\\&#39;, &#39;\\\\&#39;)&#39; >> >> # 确保正确设置模块属性(修复属性设置) >> if not hasattr(sys.modules[__name__], &#39;desktop_path&#39;): >> setattr(sys.modules[__name__], &#39;desktop_path&#39;, desktop_path) >> >> # 确保路径添加到sys.path(修复路径添加逻辑) >> if desktop_path not in sys.path: >> sys.path.append(desktop_path) >> >> # 更新PYTHONPATH环境变量(保留现有值) >> current_pythonpath = os.environ.get(&#39;PYTHONPATH&#39;, &#39;&#39;) >> if current_pythonpath: >> new_pythonpath = desktop_path + os.pathsep + current_pythonpath >> else: >> new_pythonpath = desktop_path >> os.environ[&#39;PYTHONPATH&#39;] = new_pythonpath >> >> # 显式添加路径到sys.path(双重保障) >> sys.path.insert(0, desktop_path) >> "@ >> Set-Content -Path $sitecustomizePath -Value $sitecustomizeContent -Encoding UTF8 >> >> # 创建.pth文件 >> $pthPath = Join-Path $sitePackages "desktop_path.pth" >> Set-Content -Path $pthPath -Value $desktopPath -Encoding UTF8 >> >> Write-Host "✅ Python环境修复完成" -ForegroundColor Green >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> function global:Test-PythonEnvironment { >> param([string]$PythonPath = "E:\Python310") >> >> # 修复变量名(去除中文字符) >> $pythonExe = Join-Path $PythonPath "python.exe" >> if (-not (Test-Path $pythonExe)) { >> Write-Host "❌ 找不到python.exe: $pythonExe" -ForegroundColor Red >> return >> } >> >> # 修复路径中的反斜杠问题 >> $escapedPath = $desktopPath -replace &#39;\\&#39;, &#39;\\\\&#39; >> >> # 测试Python环境(增强测试) >> $testScript = @" >> import sys >> import os >> import shlex >> import importlib.util >> >> print(f"Python版本: {sys.version}") >> print(f"系统编码: {sys.getdefaultencoding()}") >> print(f"文件系统编码: {sys.getfilesystemencoding()}") >> >> try: >> import sitecustomize >> print("✅ sitecustomize.py 加载成功") >> >> # 检查属性是否存在 >> if hasattr(sitecustomize, &#39;desktop_path&#39;): >> print(f"桌面路径: {sitecustomize.desktop_path}") >> else: >> print("❌ sitecustomize模块缺少desktop_path属性") >> >> # 尝试直接访问属性 >> try: >> print(f"尝试直接访问: {sitecustomize.desktop_path}") >> except Exception as e: >> print(f"❌ 直接访问失败: {str(e)}") >> except ImportError: >> print("❌ 无法导入 sitecustomize") >> >> print(f"标准输出编码: {sys.stdout.encoding}") >> >> desktop_path = r&#39;$escapedPath&#39; >> print(f"桌面路径在sys.path中: {desktop_path in sys.path}") >> print(f"sys.path内容: {sys.path}") >> >> py_path = os.environ.get(&#39;PYTHONPATH&#39;, &#39;未设置&#39;) >> print(f"环境变量PYTHONPATH: {py_path}") >> >> # 测试路径包含空格的情况 >> try: >> test_path = os.path.join(desktop_path, "test file with spaces.txt") >> with open(test_path, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f: >> f.write("这是一个包含空格路径的测试文件") >> >> if os.path.exists(test_path): >> print(f"✅ 成功创建包含空格的路径文件: {test_path}") >> os.remove(test_path) >> else: >> print(f"❌ 创建包含空格的路径文件失败") >> except Exception as e: >> print(f"❌ 处理包含空格的路径失败: {str(e)}") >> >> # 测试模块导入功能 >> try: >> test_module = "test_module_123" >> test_path = os.path.join(desktop_path, test_module + ".py") >> with open(test_path, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f: >> f.write("def hello(): return &#39;Hello from desktop!&#39;") >> >> spec = importlib.util.spec_from_file_location(test_module, test_path) >> test_mod = importlib.util.module_from_spec(spec) >> spec.loader.exec_module(test_mod) >> print(f"✅ 成功导入桌面模块: {test_mod.hello()}") >> >> os.remove(test_path) >> except Exception as e: >> print(f"❌ 导入桌面模块失败: {str(e)}") >> if &#39;test_path&#39; in locals() and os.path.exists(test_path): >> os.remove(test_path) >> "@ >> >> $tempScript = [System.IO.Path]::GetTempFileName() + ".py" >> Set-Content -Path $tempScript -Value $testScript -Encoding UTF8 >> >> Write-Host "`n运行环境测试..." -ForegroundColor Cyan >> try { >> & $pythonExe $tempScript >> } catch { >> Write-Host "❌ 执行测试脚本失败: $_" -ForegroundColor Red >> } finally { >> Remove-Item $tempScript -ErrorAction SilentlyContinue >> } >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 4. 主执行逻辑 PS C:\Users\Administrator\Desktop> param([string]$PythonPath = "E:\Python310") PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 执行修复 PS C:\Users\Administrator\Desktop> Repair-PythonSite -PythonPath $PythonPath ✅ Python环境修复完成 PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 5. 保存脚本到多个位置 PS C:\Users\Administrator\Desktop> $possiblePaths = @( >> [Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop), >> "C:\Users\Administrator\Desktop", >> "C:\Users\Administrator\Desktop_Backup", >> "C:\Temp" >> ) PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> foreach ($path in $possiblePaths) { >> if (Test-Path $path) { >> $scriptPath = Join-Path $path "PythonEnvRepair_Final_Complete.ps1" >> Set-Content -Path $scriptPath -Value $MyInvocation.MyCommand.Definition -Encoding UTF8 >> Write-Host "脚本已保存到: $scriptPath" -ForegroundColor Cyan >> } >> } 脚本已保存到: C:\Users\Administrator\Desktop\PythonEnvRepair_Final_Complete.ps1 脚本已保存到: C:\Users\Administrator\Desktop\PythonEnvRepair_Final_Complete.ps1 脚本已保存到: C:\Users\Administrator\Desktop_Backup\PythonEnvRepair_Final_Complete.ps1 脚本已保存到: C:\Temp\PythonEnvRepair_Final_Complete.ps1 PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 6. 最终提示 PS C:\Users\Administrator\Desktop> Write-Host "`n✅ 修复完成!请使用以下命令测试环境:" -ForegroundColor Green ✅ 修复完成!请使用以下命令测试环境: PS C:\Users\Administrator\Desktop> Write-Host "Test-PythonEnvironment -PythonPath &#39;$PythonPath&#39;" -ForegroundColor Cyan Test-PythonEnvironment -PythonPath &#39;E:\Python310&#39; PS C:\Users\Administrator\Desktop> .\PythonEnvRepair_Final_Complete.ps1 脚本已保存到: C:\Users\Administrator\Desktop\PythonEnvRepair_Final_Complete.ps1 脚本已保存到: C:\Users\Administrator\Desktop\PythonEnvRepair_Final_Complete.ps1 脚本已保存到: C:\Users\Administrator\Desktop_Backup\PythonEnvRepair_Final_Complete.ps1 脚本已保存到: C:\Temp\PythonEnvRepair_Final_Complete.ps1 PS C:\Users\Administrator\Desktop> Test-PythonEnvironment -PythonPath &#39;E:\Python310&#39; 运行环境测试... Python版本: 3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)] 系统编码: utf-8 文件系统编码: utf-8 ✅ sitecustomize.py 加载成功 ❌ sitecustomize模块缺少desktop_path属性 ❌ 直接访问失败: module &#39;sitecustomize&#39; has no attribute &#39;desktop_path&#39; 标准输出编码: utf-8 桌面路径在sys.path中: False sys.path内容: [&#39;C:\\Users\\Administrator\\AppData\\Local\\Temp&#39;, &#39;E:\\AI_System&#39;, &#39;E:\\Python310\\python310.zip&#39;, &#39;E:\\Python310\\DLLs&#39;, &#39;E:\\Python310\\lib&#39;, &#39;E:\\Python310&#39;, &#39;E:\\Python310\\lib\\site-packages&#39;] 环境变量PYTHONPATH: E:\AI_System ✅ 成功创建包含空格的路径文件: C:\\\\Users\\\\Administrator\\\\Desktop\test file with spaces.txt ✅ 成功导入桌面模块: Hello from desktop! PS C:\Users\Administrator\Desktop>
最新发布
08-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值