Python2和Python3之间的str处理方式导致乱码的讲解

Python字符串问题

  1. 在arcpy中版本为 python2.x
  2. 在QGIS中版本为 python2.x 或者 python3.x
  3. python2 和python3 之间的str处理方式经常会导致乱码,故出此文

python3版本

# 将str或字节并始终返回str
def to_str(bytes_or_str):
  if isinstance(bytes_or_str, bytes):
    value = bytes_or_str.decode(‘utf-8')
  else:
    value = bytes_or_str
  return value
# 将str或字节并始终返回bytes
def to_bytes(bytes_or_str):
  if isinstance(bytes_or_str, str):
    value = bytes_or_str.encode(‘utf-8')
  else:
    value = bytes_or_str
  return value

python2版本

- 在python2版本中使用unicode方式

# 接受str或unicode,并总是返回unicode
def to_unicode(unicode_or_str):
  if isinstance(unicode_or_str, str):
    value = unicode_or_str.decode(‘utf-8')
  else:
    value = unicode_or_str
  return value
# 接受str或unicode,并总是返回str
def to_str(unicode_or_str):
  if isinstance(unicode_or_str, unicode):
    value = unicode_or_str.encode(‘utf-8')
  else:
    value = unicode_or_str
  return value

备注

在python中不管任何版本,都是用 bytes的方式进行读取 写入会极大程度降低出现文本问题

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

(0)

相关推荐

  • Python高斯消除矩阵

    高斯消除矩阵 #! /usr/bin/env python # -*- coding: utf-8 -*- # def pprint(A): for i in A: print(i) print("") data = [ [1, 2, 1, 2], [3, 8, 1, 12], [0, 4, 1, 2] ] n = len(data) print("输入数据") pprint(data) for i in range(n): print("第{}次操作&q

  • Python最小二乘法矩阵

    最小二乘法矩阵 #! /usr/bin/env python # -*- coding: utf-8 -*- import numpy as np def calc_left_k_mat(k): """ 获得左侧k矩阵 :param k: :return: """ k_mat = [] for i in range(k + 1): now_line = [] for j in range(k + 1): now_line.append(j + i

  • python对于requests的封装方法详解

    由于requests是http类接口的核心,因此封装前考虑问题比较多: 1. 对多种接口类型的支持: 2. 连接异常时能够重连: 3. 并发处理的选择: 4. 使用方便,容易维护: 当前并未全部实现,后期会不断完善.重点提一下并发处理的选择:python的并发处理机制由于存在GIL的原因,实现起来并不是很理想,综合考虑多进程.多线程.协程,在不考虑大并发性能测试的前提下使用了多线程-线程池的形式实现.使用的是 concurrent.futures模块.当前仅方便支持webservice接口. #

  • Python中GeoJson和bokeh-1的使用讲解

    GeoJson 文档 { "type": "FeatureCollection", "features": [ { "geometry": { "type": "Polygon", "coordinates": [ [ [ 3, 1 ], [ 3, 2 ], [ 4, 2 ], [ 4, 1 ], [ 3, 1 ] ] ] }, "type": &

  • Python调用服务接口的实例

    如下所示: #! /usr/bin/env python # coding=utf-8 ###################################################################### # Author: yini.xie # Create Time: 2016-07-05 16:28:42 # Descriptioin: #################################################################

  • python 调用有道api接口的方法

    初学python ,研究了几天,写了一个python 调用 有道api接口程序 效果看下图: 申明:代码仅供和我一样的初学者学习交流 有道api申请地址http://fanyi.youdao.com/openapi?path=data-mode 申请很简单的 ps:审核不用花时间的,请勿滥用!! #-*- coding: UTF-8 -*- import urllib import urllib2 import requests import json import sys reload(sys

  • Python多图片合并PDF的方法

    python多图片合并pdf 起因 一个做美工的朋友需要将多个图片jpg .png 合并起来,PS操作太慢了所以用了python进行完成这个任务 代码 #!/usr/bin/env python # -*- coding: utf-8 -*- # @File : 2.py # @Author: huifer # @Date : 2018/12/20 from PIL import Image import os def rea(pdf_name): file_list = os.listdir(

  • 对python调用RPC接口的实例详解

    要调用RPC接口,python提供了一个框架grpc,这是google开源的 rpc相关文档: https://grpc.io/docs/tutorials/basic/python.html 需要安装的python包如下: 1.grpc安装 pip install grpcio 2.grpc的python protobuf相关的编译工具 pip install grpcio-tools 3.protobuf相关python依赖库 pip install protobuf 4.一些常见原型的生成

  • 浅谈python可视化包Bokeh

    本文研究的主要是python可视化包Bokeh的相关内容,具体如下. 问题:需要把pandas的数据绘图并通过网页显示,matplotlib需要先保存图像,不合适. 解决:在网上搜了一下,找到一篇介绍文章 python可视化工具概述,其中介绍了几个python包,总结如下: Pandas对于简单绘图,可以随手用,但你需要学习定制matplotlib. Seaborn可以支持更多复杂的可视化方式,但仍然需要matplotlib知识,上色功能是个亮点. ggplot有很多功能,但还需要发展. bok

  • Python在图片中插入大量文字并且自动换行

    问题 如何在图片中插入大量文字并且自动换行 效果 原始图 效果图 注明 若需要写入中文请使用中文字体 实现方式 from PIL import Image, ImageDraw, ImageFont class ImgText: font = ImageFont.truetype("micross.ttf", 24) def __init__(self, text): # 预设宽度 可以修改成你需要的图片宽度 self.width = 100 # 文本 self.text = text

随机推荐