中文输出

#-*-coding:utf8-*-
import requests
import re
timeout = 8
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'} def banner(url):
try:
html = requests.get(url,headers=headers,timeout=timeout)
html.encoding = 'utf-8' #这一行是将编码转为utf-8否则中文会显示乱码。 banner = re.findall(r'<title>(.*?)</title>',html.text)
return banner[0]
except Exception,e:
print e
return "no" if __name__ == "__main__":
print banner('http://www.baidu.com') 将unicode写入文本
一:
>>> f = open('1.txt','w')
>>> f.write(u'叉叉')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> a = unicode.encode(u'叉叉','utf-8')
>>> f.write(a)
>>> f.close()

二:

>>> import codecs
>>> f = codecs.open('1.txt','w')
>>> f.write(u'叉叉')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> f = codecs.open('1.txt','w','utf-8')
>>> f.write(u'叉叉')
>>> f.close()
 

python中文输出和写入文本的更多相关文章

  1. python中文编码&json中文输出问题

    python2.x版本的字符编码有时让人很头疼,遇到问题,网上方法可以解决错误,但对原理还是一知半解,本文主要介绍 python 中字符串处理的原理,附带解决 json 文件输出时,显示中文而非 un ...

  2. Windows下Python中的中文路径和中文输出问题

    这几天有个项目需要写一点类似于脚本的小程序,就用Python写了,涉及到中文路径和中文输出的问题,整理一下. 有一个问题我觉得需要先强调一下,在写Python程序的时候,一定保证编码是utf-8,然后 ...

  3. python中文注释及输出出错

    今天开始接触python,中文报错,你懂的,不细说. 网上很多类似的解决方案,有不是很明确,例如:http://blog.csdn.net/chen861201/article/details/770 ...

  4. python:创建文件夹:写入文本1:读取txt:读取Excel文件遍历文件夹:

    https://blog.csdn.net/u011956147/article/details/80369731 创建文件夹: import osimport shutil def buildfil ...

  5. 控制Python浮点数输出位数

    技术背景 在Python的一些长效任务中,不可避免的需要向文本文件.二进制文件或者数据库中写入一些数据,或者是在屏幕上输出一些文本,此时如何控制输出数据的长度是需要我们注意的一个问题.比如对于一个二进 ...

  6. 【原】python中文文本挖掘资料集合

    这些网址是我在学习python中文文本挖掘时觉得比较好的网站,记录一下,后期也会不定期添加:   1.http://www.52nlp.cn/python-%E7%BD%91%E9%A1%B5%E7% ...

  7. Eclipse搭建Python开发环境+Python中文处理

    1.基本需求 1.Eclipse 集成开发环境下载 http://115.com/file/c2vz7io5    JDK6下载 http://115.com/file/c2vz7idq     2. ...

  8. Python中文语料批量预处理手记

    手记实用系列文章: 1 结巴分词和自然语言处理HanLP处理手记 2 Python中文语料批量预处理手记 3 自然语言处理手记 4 Python中调用自然语言处理工具HanLP手记 5 Python中 ...

  9. opencv3.2将中文输出到图片上

    opencv自带的putText函数无法输出utf8类型的字符,因此无法将中文打印到图片上.用这篇文章的freetype可以实现中文输出,但是需要将字符解码转码比较麻烦,而Pillow的Image函数 ...

随机推荐

  1. oracle删除数据库中的所有数据的拼接语句

    create or replace function count_rows/**查询各表实际记录数*/(table_name in varchar2,owner in varchar2 default ...

  2. Ubuntu 14.10 下Ganglia监控Hadoop集群

    前提是已经安装好Ganglia和Hadoop集群 1 Master节点配置hadoop-metrics2.properties # syntax: [prefix].[source|sink|jmx] ...

  3. 解决办法-错误:Access denied for user 'root'@'localhost' - java

    如下更改密码即可 mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';mysql> FLUS ...

  4. php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动

    php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动 每个人机器不一样,我手头是个air book,查了一下现在最好在mac下,用mamp, mamp百科介绍 , ...

  5. BZOJ 1045 糖果传递

    奇怪的式子.最后发现取中位数. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  6. “Compiled” Python files

    To speed up loading modules, Python caches the compiled version of each module in the __pycache__ di ...

  7. Training little cats_矩阵快速幂

    Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health ...

  8. 钉子和小球_DP

    Description 有一个三角形木板,竖直立放,上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周围的钉子的距离都等于d,每个格子的宽度也都等于d,且除了最左端 ...

  9. HDU 1300

    http://acm.hdu.edu.cn/showproblem.php?pid=1300 这题大一就看到过,当时没读懂题目,今天再做就容易多了 题意:升序给出n个珍珠的的数量和价值,问买这些珍珠的 ...

  10. [转]LUA C 互调

    组件工厂 ------3D游戏研发 LUA和C之间的函数调用 1.1 从C程序调用LUA函数 LUA的函数和普通变量一样也是First Class Variable类型,可以看作函数指针变量参与栈操作 ...