参考: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. selenium获取html源代码

    # 执行js得到整个HTML html = driver.execute_script("return document.documentElement.outerHTML") 获 ...

  2. vConsole

    说明 由于移动端项目在手机中调试时不能使用chrome的控制台,而vconsole是对pc端console的改写 使用方法 使用 npm 安装: npm install vconsole 使用webp ...

  3. canvas光晕

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 获取SQL Server的安装时间

    近期安装SQL Server 2014时.还没有正式的License,仅仅能试用3个月.想知道什么时候到期,就要知道SQL Server 2014是什么时候安装的.假设你没有特意记录安装日期(实际大部 ...

  5. java分页功能代码

    import java.util.ArrayList; import java.util.List; /** * * @author cheney * * @date Aug 31, 2012 */ ...

  6. ps修图之——四步去修图后的毛边

    PS修图时,多数PS工具都会在图片的边源处留下很多毛边如下图: 这个时候很多新手店主会非常苦脑,会退回原始图片上反复修图起图.可是结果也不怎么满意,当然也许有些店主会有其它方法. 其实不用那么麻烦,只 ...

  7. Linux Load average负载详细解释

    http://tianmaotalk.iteye.com/blog/1027970     Linux Load average负载详细解释   linux查看机器负载

  8. C# JSON格式数据高级用法

     JSON简介 JSON(全称为JavaScript ObjectNotation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集.JSON采用完全独立于语言的文本格式, ...

  9. Android开发笔记之:Handler Runnable与Thread的区别详解

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一 个类只要继承了Thread类同时覆写了本类中的run( ...

  10. 使用Apache Jmeter进行并发压力测试

    http://blog.jassassin.com/2014/04/17/tools/jmeter/