同事用到一个js文件,里面的汉字都是\u这种unicode编码格式,完全看不出汉字,正好用python写个小程序,读取该文件,转为汉字后,写入另一个文件中,代码如下:
#coding=utf-8
import sys
''''''
reload(sys)
sys.setdefaultencoding('utf8')
print sys.getdefaultencoding()
file_object=open(u'C:/Users/mym/Desktop/chinaUniversityList.js','r')
try:
str=file_object.read()
except Exception,e:
print e
finally:
file_object.close()
str=str.decode('unicode-escape')
file_object2=open(u'C:/Users/mym/Desktop/1.txt','w')
try:
file_object2.write(str)
except Exception,e:
print e
finally:
file_object2.close()