【Python】如何将⽂件中xe8x85xbexe8字符转成中⽂?
解析⽂件中的内容,此⽂件内容⽐较特殊,每⾏中appName和caseName都是‘\xe8\x85\xbe\xe8’这种中⽂字符,搞了⼀下午,终于试出来了,记录⼀下。
<的内容如下:
##*startJson*##{'competitorPerfInfo':{'deviceName':'EML-AL00','appName':'\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb','detail':[{'result':[{'thres hold':3000,'type':'int','name':'start time by newInstallColdStartBySchema','value':'1311.8'}],'caseName':'\xe5\x85\xa8\xe6\x96\xb0\xe5\xae\x89\xe8\xa 3\x85\xe9\x80\x9a\xe8\xbf\x87schema\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa810\xe6\xac\xa1\xe7\xbb\x9f\xe8\xae\xa110\xe6\xac\xa1\xe5\x86\xb7\xe5\x90\x af\xe5\x8a\xa8\xe7\x9a\x84\xe5\xb9\xb3\xe5\x9d\x87\xe5\x80\xbc','conclusion':'pass'}],'testTime':1597303828,'OSVer':'27','deviceId':'CLB021832200 3470','videoKey':'','appBuildNum':111,'appVersion':'6.2.17','taskid':'432723','OS':'AND','conclusion':'pass'}}
##*startJson*##{'competitorPerfInfo':{'deviceName':'EML-AL00','appName':'\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb','detail':[{'result':[{'thres hold':3000,'type':'int','name':'start time by nonNewInstallColdStartByIcon','value':'6195.4'}],'caseName':'\xe9\x9d\x9e\xe5\x85\xa8\xe6\x96\xb0\xe5\xa
e\x89\xe8\xa3\x85\xe7\x82\xb9\xe5\x87\xbbicon\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe6\x9c\x89\xe5\xb9\xbf\xe5\x91\x8a10\xe6\xac\xa1\xe7\xbb\x9f\xe 8\xae\xa110\xe6\xac\xa1\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe7\x9a\x84\xe5\xb9\xb3\xe5\x9d\x87\xe5\x80\xbc','conclusion':'pass'}],'testTime':15973 03980,'OSVer':'27','deviceId':'CLB0218322003470','videoKey':'','appBuildNum':111,'appVersion':'6.2.17','taskid':'432723','OS':'AND','conclusion':' pass'}}
##*startJson*##{'competitorPerfInfo':{'deviceName':'EML-AL00','appName':'\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb','detail':[{'result':[{'thres hold':3000,'type':'int','name':'start time by nonNewInstallColdStartByIconNoAD','value':'1734.0'}],'caseName':'\xe9\x9d\x9e\xe5\x85\xa8\xe6\x96\xb0\x e5\xae\x89\xe8\xa3\x85\xe7\x82\xb9\xe5\x87\xbbicon\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe6\x97\xa0\xe5\xb9\xbf\xe5\x91\x8a10\xe6\xac\xa1\xe7\xbb\x 9f\xe8\xae\xa110\xe6\xac\xa1\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe7\x9a\x84\xe5\xb9\xb3\xe5\x9d\x87\xe5\x80\xbc','conclusion':'pass'}],'testTime':1 597304132,'OSVer':'27','deviceId':'CLB0218322003470','videoKey':'','appBuildNum':111,'appVersion':'6.2.17','taskid':'432723','OS':'AND','conclusi on':'pass'}}
##*startJson*##{'competitorPerfInfo':{'deviceName':'EML-AL00','appName':'\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb','detail':[{'result':[{'thres hold':3000,'type':'int','name':'start time by nonNewInstallColdStartBySchema','value':'880.4'}],'caseName':'\xe9\x9d\x9e\xe5\x85\xa8\xe6\x96\xb0\xe5\
快速排序算法举例xae\x89\xe8\xa3\x85\xe9\x80\x9a\xe8\xbf\x87schema\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa810\xe6\xac\xa1\xe7\xbb\x9f\xe8\xae\xa110\xe6\xac\xa1\xe5\x8 6\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe7\x9a\x84\xe5\xb9\xb3\xe5\x9d\x87\xe5\x80\xbc','conclusion':'pass'}],'testTime':1597304283,'OSVer':'27','deviceId':' CLB0218322003470','videoKey':'','appBuildNum':111,'appVersion':'6.2.17','taskid':'432723','OS':'AND','conclusion':'pass'}}
##*startJson*##{'competitorPerfInfo':{'deviceName':'EML-AL00','appName':'\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb','detail':[{'result':[{'thres hold':3000,'type':'int','name':'start time by newInstallColdStartByIcon','value':'4395.4'}],'caseName':'\xe5\x85\xa8\xe6\x96\xb0\xe5\xae\x89\xe8\xa3\x8 5\xe7\x82\xb9\xe5\x87\xbbicon\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe6\x97\xa0\xe5\xb9\xbf\xe5\x91\x8a10\xe6\xac\xa1\xe7\xbb\x9f\xe8\xae\xa110\xe6\ xac\xa1\xe5\x86\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe7\x9a\x84\xe5\xb9\xb3\xe5\x9d\x87\xe5\x80\xbc','conclusion':'pass'}],'testTime':1597304473,'OSVer':'2 7','deviceId':'CLB0218322003470','videoKey':'','appBuildNum':111,'appVersion':'6.2.17','taskid':'432723','OS':'AND','conclusion':'pass'}}
# -*- coding: utf-8 -*-
import sys
import json
if __name__ =='__main__':
log_path =''
if len(sys.argv)==2:
log_path = sys.argv[1]# ⽂件路径
print(log_path)
if log_path:
with open(log_path,'r')as f:# 参数encoding = 'utf-8'防⽌出现乱码
for line adlines():易语言怎么写机器人插件
# 将每⾏前⾯的特殊标记字符串"##*startJson*##"替换成空格
line_content = place("##*startJson*##",'')
# 将⾏内的单引号替换成双引号,单斜杠替换成双斜杠。如果不替换的话,json.loads时会报错
line_content = place("'",'"').replace('\\','\\\\')
nginx配置超时时间# json.loads()会将str类型转成字典类型,字典类型⽅便查key和value
string_to_dict = json.loads(line_content)
# 得到appName的value值:'\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb'
app_name = string_to_dict['competitorPerfInfo']['appName']
print(de('raw_unicode_escape'))
app_name_byte =bytes(app_name, encoding='utf-8')# 字符串类型的变量one转成bytes类型print(type(app_name_byte))# 打印查看是否是bytes类型
print("==================")python解析json文件
# 将bytes类型b'\\xe8\\x85\\xbe\\xe8\\xae\\xaf\\xe6\\x96\\xb0\\xe9\\x97\\xbb' 中的双反斜杠替换成单斜杠# 此步骤最为关键,没有此步骤的话,仍然不能打印出来汉字
get_app_name =eval(repr(app_name_byte).replace('\\\\','\\'))
print("哈哈哈"+get_app_name.decode("utf-8"))
print("==================")
print(get_app_name.decode("utf-8"))
结果是:
C:\Users\baosenzhang\AppData\Local\Programs\Python\ C:/Users/baosenzhang/PycharmProjects/NewsPerformanceData/analyseper f.
<
\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb
<class'bytes'>
==================
哈哈哈腾讯新闻
==================
腾讯新闻
\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb
<class'bytes'>
==================
哈哈哈腾讯新闻
==================
腾讯新闻
\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbbplsql注册码 12
<class'bytes'>
python基础代码大全黑客
==================
哈哈哈腾讯新闻
==================
腾讯新闻
\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb
<class'bytes'>
==================
哈哈哈腾讯新闻
==================
腾讯新闻
\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb
<class'bytes'>
==================
哈哈哈腾讯新闻
==================
腾讯新闻
Process finished with exit code 0

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。