平时遇到的一些问题2019.8.13

**

1.解决python中脚本出现中文字符,编译不通过,提示ascII报错

**
在文件头部加入,来指定文件使用的编码

#-\*-coding: utf-8-\*-

2.解决WSGI中指定接口的访问URL

利用environ来获取request的URL路径

urlPath = environ.get("PATH_INFO");

3.解决python的json解码问题

利用本身json库的相关函数,配合CSDN大神写的方法就能解析各种JSON标准格式的代码。

def analyze_json(jsons):

    key_value = ''

    if isinstance(jsons, dict):
        for key in jsons.keys():
            key_value = jsons.get(key)
            if isinstance(key_value, dict):
                analyze_json(key_value)
            elif isinstance(key_value, list):
                for json_array in key_value:
                    analyze_json(json_array)
            else:
                print(str(key) + " = " + str(key_value))
    elif isinstance(jsons, list):
        for json_array in jsons:
            analyze_json(json_array)

def _main_():
  request_body = json.loads(request_body);
  analy.analyze_json(request_body);
  request_body["usr_id"] #读取对应的Json字段

4.解决inception处理SQL中出现汉字的问题时,出现latin1报错的报错信息

将sql首先通过utf-8格式编码转成字节码,然后通过latin1解码,这样就可以适应inception处理了。

request_body["sql"].encode('utf-8').decode('latin1')

5.解决python使用utf-8时,字符串有u’ 导致出错

str.encode('gbk')

6.解决python字符串中出现’ , "和换行符的问题

通过 """ str """ 或者 ''' str ''' 来解决

7.python定义和返回一个Json字符串,并以utf-8格式来编码

resultJson = [
            {
                "id": row[0],
                "stage": row[1].replace("'","\""),
                "errlevel": row[2],
                "stagestatus": row[3].replace("'","\""),
                "errormessage": row[4],
                "sql": row[5],
                "affected_rows": row[6],
                "sequence": row[7].replace("'",""),
                "backup_dbname": row[8].replace("'","\""),
                "execute_time": row[9].replace("'","\""),
                "SQLSHA1": row[10].replace("'","\""),
                "user_id": ExeData.usr_id,
                "sys_information": ExeData.sys_information,
                "service_information": ExeData.service_information,
                "version_information": ExeData.version_information
            }
            for row in result
        ]
return [json.dumps(response).encode("utf8")];

8.解决python同时遍历两个list的问题

利用zip()方法来实现同时遍历的功能

sequence,backup_db_name in zip(BackupData.sequence_list,BackupData.backup_db_name_list):

9.实现pymysql的事务性操作

try:
	rows+=cursor.execute(sqlList[i]);
except Exception as e2:
 	db.rollback();
  	print("commit failure,please try again");
else:
  	db.commit();

你可能感兴趣的:(杂谈,Python)