【python】字符串编码问题
参考: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】字符串编码问题的更多相关文章
- python 字符串编码
通过字符串的decode和encode方法 1 encode([encoding,[errors]]) #其中encoding可以有多种值,比如gb2312 gbk gb18030 bz2 zlib ...
- 不得不知道的Python字符串编码相关的知识
开发经常会遇到各种字符串编码的问题,例如报错SyntaxError: Non-ASCII character 'ascii' codec can't encode characters in posi ...
- python字符串编码理解(转载)
(转载)字符编码和python使用encode,decode转换utf-8, gbk, gb2312 (http://www.cnblogs.com/jxzheng/p/5186490.html) A ...
- 【转载】不得不知道的Python字符串编码相关的知识
原文地址:http://www.cnblogs.com/Xjng/p/5093905.html 开发经常会遇到各种字符串编码的问题,例如报错SyntaxError: Non-ASCII charact ...
- python字符串编码
python默认编码 python 2.x默认的字符编码是ASCII,默认的文件编码也是ASCII. python 3.x默认的字符编码是unicode,默认的文件编码是utf-8. 中文乱码问题 无 ...
- Python字符串编码——Unicode
ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte).也就是 ...
- Python字符串编码问题
编码问题:Unicode把所有语言都统一到一套编码里,这样就不会再有乱码问题了. ASCII编码和Unicode编码的区别:ASCII编码是1个字节,而Unicode编码通常是2个字节.字母A用ASC ...
- python 字符串编码 ,区别 utf-8 和utf-8-sig
Python 读取文件首行多了"\ufeff"字符串 python读取B.txt文件时,控制台打印首行正常,但是若是用首行内容打开文本的话,就会报错: Traceback (mos ...
- Python字符串编码转换
使用encode()方法编码 str.encode([encoding="utf-8"][,errors="strict"]) str:表示需要转换的字符串 e ...
- python 字符串编码转换
import chardetdef CheckCode(filename): adchar=chardet.detect(filename) if adchar['encoding']=='utf-8 ...
随机推荐
- 复制VirtualBox中的虚拟机
假设简单的复制虚拟机是行不通的.复制过程须要一个小技巧,复制出来的VDI文件无法在虚拟介质管理器中注冊.由于每一个VDI文件都有一个唯一的uuid.而VirtualBox不同意注冊反复的uuid. 为 ...
- 算法笔记_087:蓝桥杯练习 9-1九宫格(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 九宫格.输入1-9这9个数字的一种任意排序,构成3*3二维数组.如果每行.每列以及对角线之和都相等,打印1.否则打印0. 样例输出 与上面 ...
- 从cocos2dx到cocos2dhtml5的不同之处
首先cocos2dhtml5使用javascript编程, 严格区分大写和小写. 1.新建cocos2dhtml5项目. 直接复制引擎自带的helloworld.改一下目录名字就可以. 2.新增js文 ...
- easyui datebox 扩展清空按钮
/** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; (function ($) { var buttons = $.ext ...
- hibernate 一对多双向的CURD
转自尚学堂教学视频 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import java ...
- hibernate 关系映射之 单向外键关联一对一
这里的关系指的是对象与对象之间的关系 注解方式单向关联一对一: //这个类描述的husband是一个对应一个wife的 import javax.persistence.Entity; import ...
- 萌新学习Python爬取B站弹幕+R语言分词demo说明
代码地址如下:http://www.demodashi.com/demo/11578.html 一.写在前面 之前在简书首页看到了Python爬虫的介绍,于是就想着爬取B站弹幕并绘制词云,因此有了这样 ...
- [转]SIGHUP与终端控制
SIGHUP信号与控制终端 UNIX中进程组织结构为 session (会话)包含一个前台进程组及一个或多个后台进程组,一个进程组包含多个进程.一个session可能会有一个session首进程, ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 【转载】XGBoost调参
General Parameters: Guide the overall functioning Booster Parameters: Guide the individual booster ( ...