2017-08-14 8 views
-1

をPYTHONするのpython 3スクリプトを有効にする必要があり、私はこのPythonの3のスクリプトを持っていたし、今私はそれがurlib2に変更する必要があり、私はurilibを読んだものから、Pythonの2、 上で実行する必要があります。 しかし、エンコードクラスに変更があったようです。 私はこのエラーを取得するために:は2.7スクリプト

16:29:27 + python p.py 
16:29:27 Traceback (most recent call last): 
16:29:27 File "p.py", line 20, in <module> 
16:29:27  binary_data = params.encode(encoding=repr,errors=strict) 
16:29:27 NameError: name 'strict' is not defined 

これは私のPython 3スクリプトです:

import urllib 
import urllib.request 
import sys 
import os 
url = 'http://google.com' 
params = urllib.parse.urlencode({ 
'profile': 'dev' , 
'KEY':'bdi.key' , 
}) 
binary_data = params.encode(encoding='ascii',errors='strict') 
response = urllib.request.urlopen(url, binary_data).read() 

これは私のPython 2スクリプトです:

import urllib 
import urllib2 
from contextlib import closing 
import sys 
import os 
url = 'http://google.com' 
params = urllib.urlencode({ 
'profile': 'dev' , 
'KEY':'bdi.key' , 
}) 
binary_data = params.encode(encoding=repr,errors=strict) 
response = urllib2.urlopen(url, binary_data).read() 

どうやって 私はそれを働かせることができますか? これまでPythonを使用したことはありません。これは保守しようとしているメンテナンススクリプトです:)

+1

'binary_data = params.encode(エンコーディング= 'ASCII'、エラー= '厳密')'? Pythonに単語ではなく文字列であることを伝えるために、wordc 'strict'と' repr'の前後に引用符 '' 'を使用しました。 –

+0

'strict'は' string'でなければなりません エンコーディングは文字列( '' ascii "')でなければなりません。 –

答えて

0

問題は文字列エンコーディング関数のerrorsです。変数strictは存在しないので、strictを引用符で囲む必要があります。

だから、それはこのようにする必要があります:

binary_data = params.encode(encoding=repr, errors='strict')