from urllib.parse import quote, unquote
# 修改字符串编码
text = "http://zhishi.me/zhwiki/resource/%E5%90%95%E9%BE%99%E5%85%89"
# print(unquote(""""\u897f\u683c\u8bfa\u745e\u4e9a\u516c\u5bd3\u9152\u5e97" ."""))
print(text.encode('raw_unicode_escape').decode())
# url 解码 类似:http://zhishi.me/zhwiki/resource/%E5%90%95%E9%BE%99%E5%85%89
# 使用 unquote
def url_parse_file2unicode(src_path, new_file, infile):
open_diff = open('%s\%s' % (src_path, infile), 'r', encoding='utf-8')
line_num = open_diff.readlines()
with open('%s\%s' % (src_path, new_file), 'w', encoding='utf-8') as file:
for line in line_num:
file.write(unquote(line))
file.close()
open_diff.close()
# url 编码
# 使用 quote ,将其变为类似于:http://zhishi.me/zhwiki/resource/%E5%90%95%E9%BE%99%E5%85%89
def url_parse_file(src_path, new_file, infile):
open_diff = open('%s\%s' % (src_path, infile), 'r', encoding='utf8')
line_num = open_diff.readlines()
with open('%s\%s' % (src_path, new_file), 'w', encoding='utf8') as file:
for line in line_num:
file.write(quote(line))
# 类似:http://cndbpedia/ontology/\u5b9e\u4f53\u540d\u79f0
# 使用 unicode-escape
def decode_parse_file(src_path, new_file, infile):
open_diff = open('%s\%s' % (src_path, infile), 'r')
line_num = open_diff.readlines()
open_diff.readline()
with open('%s\%s' % (src_path, new_file), 'w', encoding='utf-8') as file:
for line in line_num:
# 先编码再解码
file.write(unquote(line.encode().decode(encoding='unicode-escape')))
if __name__ == '__main__':
src_path = "C:\\Users\\my\\Desktop\\NLP"
infile = "cndbpediaDump_26.nt"
new_file = "new_%s" % infile
# 如果含有 % ,则使用 unquote
if open('%s\%s' % (src_path, infile), 'r', encoding='utf-8').readline().find("%") > 0:
url_parse_file2unicode(src_path, new_file, infile)
# 如果没有 % ,则使用 unicode-escape
else:
decode_parse_file(src_path, new_file, infile)
总结
str.encode()
把一个字符串转换为其 raw bytes
形式
bytes.decode()
把 raw bytes
转换为其字符串形式
遇到类似的编码问题时,先 text
是什么类型,如果 type(text) is bytes
,那么
text.decode('unicode-escape')
如果 type(text) is str
,那么(将 Unicode
的内存编码值进行存储,读取文件时反向转换回来。这里就采用了 unicode-escape
的方式)
text.encode().decode('unicode-escape')