2017-07-02 85 views
0

這是我的一段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

回答

1

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') 
+0

提斯釋放出另一種'「字符表」編解碼器的誤差不能編碼字符U「\ X82」在位置21:字符映射爲' – Abra001

+0

我使用python2反正我知道它解釋,因此,我會嘗試與python3 – Abra001