PythonでURLの‘%2B 20%’‘3B’をエンコーディングする

Pythonコード:
import os,sys;

path = sys.path[0]
os.chdir(path)
encode_list = 'encode_list.txt’
result = path + '\\results’

def get_encode():  //エンコーディング
encode_file = open(path + '\\’+ encode_list)
encode = dict()
for line in encode_file:
if line!=’\n’ and len(line) >1:
if line.find('read me’) <0:
encode[line[1:].strip()] = line[0]
return encode

def get_files(): //ファイルを取得
files = os.listdir(path)
file_list = list()
for file in files:
if file.endswith('.txt’) and file!= encode_list:
file_list.append(file)
return file_list

def relace_url_encode(strPri,dicEncode): //URLをエンコーディング
items = dicEncode.items()
for (key,value) in items:
if strPri.find(key):
strPri = strPri.replace(key,value)
return strPri

def create_result():  //結果を作成
if not os.path.isdir(result):
os.makedirs(result)

def write_result(filePri,strText):  //結果を書き込む
fp = open(result+’\\’+filePri,’w+’)
fp.write(strText)
fp.close()

create_result()
encode = get_encode()
file_list = get_files()
for ff in file_list:
try:
f = open(ff)
text = f.read()
finally:
f.close()
temp = relace_url_encode(text,encode)
temp = temp.replace('&’,’\n’)
write_result(ff,temp)

Python

Posted by arkgame