参考: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. c# 句柄数不断攀升的解决方案

    句柄只是用来标识应用程序中的不同对象和同类中的不同的实例的一个数字,通常情况下,句柄值对普通用户毫无用处,但是句柄数量却可以间接反映出一个程序里产生的对象实例的多少.句柄数越多,代表程序里new 出来 ...

  2. js设置加载进度提示

      CreateTime--2017年8月23日09:17:46Author:Marydon js设置加载进度提示 第一部分:CSS /*加载样式*/ .Loading { position: abs ...

  3. 【服务器时间修改为东八区】包括Apache2和mysql

    1.服务器Apache时间修改,可通过修改php.ini进行修改 所以就深入了解了一下, 发现Apache(PHP)的服务器时间时区默认为UTC(Coordinated UniversalTime 世 ...

  4. Android Shell Cmd

    1. view android version: grep ro.build.version.sdk= system/build.prop getprop ro.build.version.relea ...

  5. python3安装scrapy问题解决

    scrapy py3.5安装 pip install scrapy 直接安装失败 第一个错误 building 'lxml.etree' extension 使用 pip install lxml 安 ...

  6. mybatis想要在控制台显示sql语句配置文件

    在src目录下创建一个properties文件 配置内容如下 log4j.rootLogger=debug,stdout,logfile log4j.appender.stdout=org.apach ...

  7. 使用 GROUP BY WITH ROLLUP 改善统计性能

    使用 GROUP BY 的 WITH ROLLUP 字句可以检索出更多的分组聚合信息,它不仅仅能像一般的 GROUP BY 语句那样检索出各组的聚合信息,还能检索出本组类的整体聚合信息. 下面我们的例 ...

  8. Atitit。Js调用后台语言 java c#  php swing android  swt的方法大总结

    Atitit.Js调用后台语言 java c#  php swing android  swt的方法大总结 1. Js调用后台语言有三种方法1 2. Swt  BrowserFunction 绑定方法 ...

  9. Atitit.执行cmd 命令行 php

    Atitit.执行cmd 命令行 php 1. 执行cmd 命令行,调用系统命令的基础 1 1.1. 实际执行模式 1 1.2. 空格的问题 1 1.3. 中文路径的问题,程序文件读取编码设置 1 1 ...

  10. 设计模式_EventObject和EventListener

    一.事件机制基本概念 java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2. ...