【Python】如何将文件中\xe8\x85\xbe\xe8字符转成中文?

解析文件resultjsonlog.txt中的内容,此文件内容比较特殊,每行中appName和caseName都是‘\xe8\x85\xbe\xe8’这种中文字符,搞了一下午,终于试出来了,记录一下。
resultjsonlog.txt的内容如下:

##*startJson*##{'competitorPerfInfo': {'deviceName': 'EML-AL00', 'appName': '\xe8\x85\xbe\xe8\xae\xaf\xe6\x96\xb0\xe9\x97\xbb', 'detail': [{'result': [{'threshold': 3000, 'type': 'int', 'name': 'start time by newInstallColdStartBySchema', 'value': '1311.8'}], 'caseName': '\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\x86\xb7\xe5\x90\xaf\xe5\x8a\xa8\xe7\x9a\x84\xe5\xb9\xb3\xe5\x9d\x87\xe5\x80\xbc', 'conclusion': 'pass'}], 'testTime': 1597303828, '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': [{'threshold': 3000, 'type': 'int', 'name': 'start time by nonNewInstallColdStartByIcon', 'value': '6195.4'}], 'caseName': '\xe9\x9d\x9e\xe5\x85\xa8\xe6\x96\xb0\xe5\xae\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\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': 1597303980, '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': [{'threshold': 3000, 'type': 'int', 'name': 'start time by nonNewInstallColdStartByIconNoAD', 'value': '1734.0'}], 'caseName': '\xe9\x9d\x9e\xe5\x85\xa8\xe6\x96\xb0\xe5\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\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': 1597304132, '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': [{'threshold': 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\x86\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': [{'threshold': 3000, 'type': 'int', 'name': 'start time by newInstallColdStartByIcon', 'value': '4395.4'}], 'caseName': '\xe5\x85\xa8\xe6\x96\xb0\xe5\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\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': '27', '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] # resultjsonlog.txt文件路径
        print(log_path)
    if log_path:
        with open(log_path, 'r') as f:  # 参数encoding = 'utf-8'防止出现乱码
            for line in f.readlines():
                # 将每行前面的特殊标记字符串"##*startJson*##"替换成空格
                line_content = line.replace("##*startJson*##", '')
                # 将行内的单引号替换成双引号,单斜杠替换成双斜杠。如果不替换的话,json.loads时会报错
                line_content = line_content.replace("'", '"').replace('\\', '\\\\')
                # 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(app_name.encode('raw_unicode_escape'))
                app_name_byte = bytes(app_name, encoding='utf-8')  # 字符串类型的变量one转成bytes类型
                print(type(app_name_byte))   # 打印查看是否是bytes类型
                print("==================")
                # 将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\Python37\python.exe C:/Users/baosenzhang/PycharmProjects/NewsPerformanceData/analyseperf.py resultjsonlog.txt
resultjsonlog.txt
\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\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\xbb
<class 'bytes'>
==================
哈哈哈腾讯新闻
==================
腾讯新闻

Process finished with exit code 0

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值