Windows terminal powershell7解决输出打印中文时出现乱码

img

最近闲来无事,将 powershell 7.2.5 升级到了 powershell 7.4.5。遇到这样一个问题,输出打印中文时出现乱码。

PS D:\work> cat .\plw.txt
��ǰʱ��: 2024-09-25 20:00:49.586130
�������һע���������: 12346 --- ��ʽ����ĵ�ǰʱ��: 2024-09-25 20:00:49

值得一提的是 powershell 7.4.5 命令提示和自动补全体验很友好。敲过的历史命令保存在 history 文件中,再次使用时会加载进行提示。

img

我当前使用到 powershell 版本:

PS E:\AI\StableSwarmUI-0.6.4-Beta\StableSwarmUI> pwsh
PowerShell 7.4.5

关于字符编码标准,可以参考 Unicode 标准

Unicode 是一种全球字符编码标准。 系统将 Unicode 专用于字符和字符串操作。 有关 Unicode 的各个方面的详细说明,请参阅 Unicode 标准

测试,执行 Python 脚本使用重定向运算符 >> 追加输出至文件 plw.txt 保存内容,配合 typegccatGet-ContentSelect-String 命令读取内容并输出到终端显示。

Windows 中的兼容性别名。PowerShell 具有多个别名,使 UNIX 和 cmd.exe 用户可以在 Windows 中使用熟悉的命令。 下表显示了常用命令、相关的 PowerShell cmdlet 和 PowerShell 别名:

Windows 命令行界面UNIX 命令PowerShell CmdletPowerShell 别名
cdchdircdSet-Locationslcdchdir
clsclearClear-Hostcls clear
copycpCopy-Itemcpicpcopy
deleraserdrmdirrmRemove-Itemrideleraserdrmrmdir
dirlsGet-ChildItemgcidirls
echoechoWrite-Outputwrite echo
mdmkdirNew-Itemni
movemvMove-Itemmimovemi
popdpopdPop-Locationpopd
pwdGet-Locationglpwd
pushdpushdPush-Locationpushd
renmvRename-Itemrniren
typecatGet-Contentgccattype

解决 PowerShell 字符编码乱码

指定当前输出文件字符编码 -Encoding ansi

当然,你还可以指定字符编码的数字代码,比如 ansi 默认活动代码页为 936,指定当前输出文件字符编码 -Encoding 936。

Windows terminal 打开 powershell7.4.5 使用 typeGet-Contentgccat 测试显示效果是一样的,如下使用 catGet-Contentgctype 命令作为演示。

关于 Select-String 命令参数,此处做简单说明,Select-String 会将 “content” 引号里面的内容增强显示,用白色背景包裹,字体以黑色展示。

cat 命令形式,指定参数 -Encoding ansiSelect-String

img

PS D:\work> .\test\plw.py >> .\plw.txt && cat .\plw.txt -Encoding ansi | Select-String "20:00"
PS D:\work> .\test\plw.py >> .\plw.txt && cat .\plw.txt -Encoding 936 | Select-String "20:00"

输出内容,当前时间: 2024-09-25 20:00:49.586130 随机生成一注排列五号码: 12346 — 格式化后的当前时间: 2024-09-25 20:00:49

Get-Content 命令形式,指定参数 -Encoding ansiSelect-String

img

PS D:\work> .\test\plw.py >> .\plw.txt && Get-Content .\plw.txt -Encoding ansi | Select-String "20:30"
PS D:\work> .\test\plw.py >> .\plw.txt && Get-Content .\plw.txt -Encoding 936 | Select-String "20:30"

输出内容,当前时间: 2024-09-25 20:30:15.973906 随机生成一注排列五号码: 57127 — 格式化后的当前时间: 2024-09-25 20:30:15

gc 命令形式,指定参数 -Encoding ansiSelect-String

img

PS D:\work> gc .\plw.txt | Select-String "2024-09-25 20:00:49"
��ǰʱ��: 2024-09-25 20:00:49.586130
�������һע���������: 12346 --- ��ʽ����ĵ�ǰʱ��: 2024-09-25 20:00:49

PS D:\work> gc .\plw.txt -Encoding ansi | Select-String "2024-09-25 20:00:49"
当前时间: 2024-09-25 20:00:49.586130
随机生成一注排列五号码: 12346 --- 格式化后的当前时间: 2024-09-25 20:00:49

type 命令形式,指定参数 -Encoding ansiSelect-String

img

PS D:\work> type .\plw.txt | Select-String "2024-09-25 20:00:49"
��ǰʱ��: 2024-09-25 20:00:49.586130
�������һע���������: 12346 --- ��ʽ����ĵ�ǰʱ��: 2024-09-25 20:00:49

PS D:\work> type .\plw.txt -Encoding ansi | Select-String "2024-09-25 20:00:49"
当前时间: 2024-09-25 20:00:49.586130
随机生成一注排列五号码: 12346 --- 格式化后的当前时间: 2024-09-25 20:00:49

文初显示中文内容乱码,此处演示指定参数 -Encoding ansi 中文正常显示,契合 Windows 平台 PowerShell 默认活动代码页 936 。

这只是其中一种解决方案,也可能存在其它更优解决方案。

测试使用 Python 脚本如下。

import random
from datetime import datetime  

# 生成一个五位数的随机数,允许以 0 开头  
num = random.randint(0, 99999)

# 确保总是五位数(通过格式化)  
num_str = f"{num:05d}"       

# 获取当前时间  
now = datetime.now()  

# 打印当前时间  
print("当前时间:", now)  

# 特定的格式打印时间,可以使用 strftime 方法  
# 例如,以 YYYY-MM-DD HH:MM:SS 的格式打印  
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")   

print("随机生成一注排列五号码:", num_str, "---" ,"格式化后的当前时间:", formatted_now)

PowerShell 中的字符编码

在 PowerShell(v7.1 及更高版本)中,Encoding 参数支持以下值:

  • ascii:对 ASCII(7 位)字符集使用编码。
  • ansi:对当前区域性的 ANSI 代码页使用编码。 此选项是在 PowerShell 7.4 中添加的。
  • bigendianunicode:使用 big-endian 字节顺序以 UTF-16 格式进行编码。
  • bigendianutf32:使用 big-endian 字节顺序以 UTF-32 格式进行编码。
  • oem:对 MS-DOS 和控制台程序使用默认编码。
  • unicode:使用 little-endian 字节顺序以 UTF-16 格式进行编码。
  • utf7:采用 UTF-7 格式编码。
  • utf8:采用 UTF-8 格式(无 BOM)进行编码。
  • utf8BOM:使用字节顺序标记 (BOM) 以 UTF-8 格式进行编码
  • utf8NoBOM:不使用字节顺序标记 (BOM) 以 UTF-8 格式进行编码
  • utf32:使用 little-endian 字节顺序以 UTF-32 格式进行编码。

PowerShell 默认对所有输出都使用 utf8NoBOM

从 PowerShell 6.2 开始,Encoding 参数还允许注册代码页的数字 ID(如 -Encoding 1251)或已注册代码页的字符串名称(如 -Encoding "windows-1251")。 有关详细信息,请参阅 Encoding.CodePage .NET 文档。

从 PowerShell 7.4 开始,可以使用 Encoding 参数的 Ansi 值来传递当前区域性 ANSI 代码页的数字 ID,而无需手动指定它。

关于 catGet-ContentSelect-String 命令更多用法,此处不做赘述,详情见文末参考资料。

最后,希望对你的学习,工作和生活有所帮助。

以上总结,仅供参考。

参考资料:

—END—

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值