Python编码问题,无法识别网络适配器

2024-06-16 11:47:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的一段python代码。你知道吗

 import os
 print 'netsh interface ip set address name="' + adapter + '" static '+ staticaddr +' 255.255.255.0 192.168.1.1'

直到这一点都是好的适配器包括在我的驱动程序。你知道吗

现在当我以管理员的身份运行这个程序时

 os.system('netsh interface ipv4 set address name="' + adapter + '" static '+ staticaddr +' 255.255.255.0 192.168.1.1')

它触发此错误:

La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.

这意味着命令的语法不正确。你知道吗

我试过os.system('netsh interface ipv4 set address name="' + adapter.encode('ascii','ignore') + '" static '+ staticaddr +' 255.255.255.0 192.168.1.1')

现在出现这种异常:

'ascii' codec can't decode byte 0x82 in position 11: ordinal not in range(128)

问题到底出在哪里?你知道吗

我的网络适配器的名称是:Connexion réseau sans fil


Tags: 代码nameinadapterosaddressasciistatic
1条回答
网友
1楼 · 发布于 2024-06-16 11:47:11

adapter.encode('ascii','ignore')引发UnicodeDecodeError,因为adapter是一个非ascii字符串。要对它进行编码(即从unicode转换为str),Python首先尝试对它进行解码(即从str转换为unicode),但失败了(adapter是非ascii)。你知道吗

完全切换到unicode:

print (u'netsh interface ip set address name="' + adapter.decode('latin1') + u'" static '+ staticaddr.decode('ascii') + u' 255.255.255.0 192.168.1.1').encode('latin1')

相关问题 更多 >