参考:http://blog.csdn.net/tingsking18/article/details/4033645

python内部的字符串是以unicode来编码

decode函数用来将其他编码解码为unicode

encode函数将unicode编码为指定的编码类型,例如gbk,utf-8

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 15 15:20:59 2014 @author: hp
""" import urllib2
import re
import time
import jieba url="http://blog.sina.com.cn/s/blog_608e1afd0102e5ym.html"
def geturl(url):
html=urllib2.urlopen(url).read()
html=unicode(html,'utf-8')
word=re.findall(ur"[\u4e00-\u9fa5]+",html) s=""
for w in word:
s+=w
return s #return web content
def separate_word(s):
seg_list=jieba.cut(s,cut_all=False)
fenci="/ ".join(seg_list)
# print 'get web-->',s
# print 'div result-》',fenci
# print "fenci[1]-->",fenci[1]
word_list=[]
word_tmp=""
#word_tmp.decode('utf-8')
for i in range(len(fenci)):
if fenci[i]!="/":
word_tmp+=fenci[i]
else:
i+=1
word_tmp.decode('utf-8','ignore')
word_list.append(word_tmp)
word_tmp=""
#word_list=seg_list.split("/ ") # print "word_list-->",word_list
return word_list def count_word(word_list):
word_list_group=[]
word_num=[]
dic={}
for i in range(len(word_list)):
w_tmp=word_list[i]
signal=0
for j in range(len(word_list_group)):
if word_list_group[j]==w_tmp:
signal=1
if signal==0:
word_list_group.append(unicode(w_tmp.encode('utf-8'),'utf-8')) for i in range(len(word_list_group)):
num=0
for j in range(len(word_list)):
if word_list_group[i]==word_list[j]:
num+=1
word_num.append(num) for i in range(len(word_list_group)):
dic[word_list_group[i].encode('gbk')]=word_num[i] # for i in range(len(word_list_group)):
# print "word_list_group-->",word_list_group[i].encode('gbk'),"word_num-->",word_num[i]
return dic
# return word_list_group,word_num contant=geturl(url)
word=separate_word(contant)
result=count_word(word)
for key in result.keys():
print key.encode('gbk'),"--->",result[key]
#print result time.sleep(10)

【python】字符串编码问题的更多相关文章

  1. python 字符串编码

    通过字符串的decode和encode方法 1 encode([encoding,[errors]]) #其中encoding可以有多种值,比如gb2312 gbk gb18030 bz2 zlib ...

  2. 不得不知道的Python字符串编码相关的知识

    开发经常会遇到各种字符串编码的问题,例如报错SyntaxError: Non-ASCII character 'ascii' codec can't encode characters in posi ...

  3. python字符串编码理解(转载)

    (转载)字符编码和python使用encode,decode转换utf-8, gbk, gb2312 (http://www.cnblogs.com/jxzheng/p/5186490.html) A ...

  4. 【转载】不得不知道的Python字符串编码相关的知识

    原文地址:http://www.cnblogs.com/Xjng/p/5093905.html 开发经常会遇到各种字符串编码的问题,例如报错SyntaxError: Non-ASCII charact ...

  5. python字符串编码

    python默认编码 python 2.x默认的字符编码是ASCII,默认的文件编码也是ASCII. python 3.x默认的字符编码是unicode,默认的文件编码是utf-8. 中文乱码问题 无 ...

  6. Python字符串编码——Unicode

    ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte).也就是 ...

  7. Python字符串编码问题

    编码问题:Unicode把所有语言都统一到一套编码里,这样就不会再有乱码问题了. ASCII编码和Unicode编码的区别:ASCII编码是1个字节,而Unicode编码通常是2个字节.字母A用ASC ...

  8. python 字符串编码 ,区别 utf-8 和utf-8-sig

    Python 读取文件首行多了"\ufeff"字符串 python读取B.txt文件时,控制台打印首行正常,但是若是用首行内容打开文本的话,就会报错: Traceback (mos ...

  9. Python字符串编码转换

    使用encode()方法编码 str.encode([encoding="utf-8"][,errors="strict"]) str:表示需要转换的字符串 e ...

  10. python 字符串编码转换

    import chardetdef CheckCode(filename): adchar=chardet.detect(filename) if adchar['encoding']=='utf-8 ...

随机推荐

  1. 算法笔记_051:荷兰国旗问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换任意两个球,使得从左至右的球依次为红球.白球.蓝球.这个问题之所以叫荷兰国旗,是因为 ...

  2. error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037

    问题原因:端口5037被占用 解决方案: 方式一:可以用cmd命令 C:\Users\Administrator>netstat -ano | findstr "5037" ...

  3. android项目解刨之时间轴

    近期开发的app中要用到时间轴这东西.须要实现的效果例如以下: 想想这个东西应该能够用listview实现吧. 然后近期就模拟着去写了: 首先写  listview的item的布局: listview ...

  4. 你不知道的js技巧

    JS进阶 说起这个应该算是老生常谈了吧.所谓的高级,其实就是讲了一些我们平常用不到(或许用了不知道),但是非常实在的东西.算是熟练掌握js的一个必经road吧. 检测函数类型 其实检测函数的类型应该算 ...

  5. IOS炫酷的引导界面

    代码地址如下:http://www.demodashi.com/demo/11246.html 一.准备工作 1.先用时ps工具制作好图片 2.然后计算好每张图片通过滑动视图的偏移量来改变图片的位置 ...

  6. ThreadLocal源码

    /* * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  7. 【转帖】Servlet 3.0 新特性详解

    http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性概述 Servlet 3.0 作为 Java EE 6 ...

  8. C# 遍历Dictionary并修改其中的Value

    C#的Dictionary类型的值,知道key后,value可以修改吗?答案是肯定能修改的.我在遍历的过程中可以修改Value吗?答案是也是肯定能修改的,但是不能用For each循环.否则会报以下的 ...

  9. Spring学习笔记(四)-- Spring事务全面分析

    通过本系列的文章对Spring的介绍,我们对Spring的使用和两个核心功能IOC.AOP已经有了初步的了解,结合我个人工作的情况,因为项目是金融系 统.那对事务的控制是不可缺少的.而且是很严格的控制 ...

  10. 【Objective-C】03-第一个OC程序

    一.打开Xcode,新建Xcode项目 二.选择最简单的命令行项目 因为我们只是学习OC语法,还未正式进入iOS开发,所以选择命令行项目即可 三.输入项目名称,选择Foundation框架进行创建项目 ...