zoukankan      html  css  js  c++  java
  • python处理u开头的字符串

    是用python处理excel过程中,从表格中解析除字符串,打印出来的中文却显示成了u'开头的乱码字符串,在控制台中输出的编码格式是utf-8,而excel表格的数据也是utf-8编码成的,但是解析成字符串则是成了一个unicode编码组成的字符串,“u”后的16进制字符串是相应汉字的utf-16编码,所以我们需要将这写字符串解码成unicode字符串。
    使用decode("unicode_escape")

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    from collections import OrderedDict
    from pyexcel_xls import get_data
    from pyexcel_xls import save_data
    import redis
    def read_xls_file():
        xls_data = get_data(r"test.xlsx")
        print "Get data type:", type(xls_data)
        conn = redis.Redis()
        for key in xls_data['sheet1']:
            key = str(key).decode("unicode_escape").encode("utf8")
            print key
            key = key.lstrip()
            key = key.rstrip()
            # conn.set(key, key)
    
    if __name__ == '__main__':
        read_xls_file()
    
  • 相关阅读:
    洛谷#P5652#基础博弈练习题
    hgoi#20191112
    hgoi#20191111
    hgoi#20191109
    洛谷#P3674#小清新人渣的本愿
    hgoi#20191108
    hgoi#20191107
    树上差分
    树链剖分(树剖)
    LCA(最近公共祖先)问题
  • 原文地址:https://www.cnblogs.com/fishisnow/p/6943621.html
Copyright © 2011-2022 走看看