python中文输出和写入文本
中文输出
#-*-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中文输出和写入文本的更多相关文章
- python中文编码&json中文输出问题
python2.x版本的字符编码有时让人很头疼,遇到问题,网上方法可以解决错误,但对原理还是一知半解,本文主要介绍 python 中字符串处理的原理,附带解决 json 文件输出时,显示中文而非 un ...
- Windows下Python中的中文路径和中文输出问题
这几天有个项目需要写一点类似于脚本的小程序,就用Python写了,涉及到中文路径和中文输出的问题,整理一下. 有一个问题我觉得需要先强调一下,在写Python程序的时候,一定保证编码是utf-8,然后 ...
- python中文注释及输出出错
今天开始接触python,中文报错,你懂的,不细说. 网上很多类似的解决方案,有不是很明确,例如:http://blog.csdn.net/chen861201/article/details/770 ...
- python:创建文件夹:写入文本1:读取txt:读取Excel文件遍历文件夹:
https://blog.csdn.net/u011956147/article/details/80369731 创建文件夹: import osimport shutil def buildfil ...
- 控制Python浮点数输出位数
技术背景 在Python的一些长效任务中,不可避免的需要向文本文件.二进制文件或者数据库中写入一些数据,或者是在屏幕上输出一些文本,此时如何控制输出数据的长度是需要我们注意的一个问题.比如对于一个二进 ...
- 【原】python中文文本挖掘资料集合
这些网址是我在学习python中文文本挖掘时觉得比较好的网站,记录一下,后期也会不定期添加: 1.http://www.52nlp.cn/python-%E7%BD%91%E9%A1%B5%E7% ...
- Eclipse搭建Python开发环境+Python中文处理
1.基本需求 1.Eclipse 集成开发环境下载 http://115.com/file/c2vz7io5 JDK6下载 http://115.com/file/c2vz7idq 2. ...
- Python中文语料批量预处理手记
手记实用系列文章: 1 结巴分词和自然语言处理HanLP处理手记 2 Python中文语料批量预处理手记 3 自然语言处理手记 4 Python中调用自然语言处理工具HanLP手记 5 Python中 ...
- opencv3.2将中文输出到图片上
opencv自带的putText函数无法输出utf8类型的字符,因此无法将中文打印到图片上.用这篇文章的freetype可以实现中文输出,但是需要将字符解码转码比较麻烦,而Pillow的Image函数 ...
随机推荐
- Ubuntu 14.10 下Server版本中文乱码问题
在安装Ubuntu server 14.10 时候选择了中文版,但是装好系统发现,里面的中文有乱码,解决办法 打开 /etc/default/locale sudo nano /etc/default ...
- vijos 1780 开车旅行
细节巨多. 倍增即可. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- linux常用命令:3文件搜索命令
文件搜索命令 1. 命令名:find 命令所在路径:/bin/find 执行权限:所有用户 语法:find [搜索范围] [匹配条件] 功能描述:文件搜索 文件搜索类型 通过文件名搜索 -name ...
- DXUT初步理解
关于DXUT框架: DXUT(The DirectX Utility Library) 是建立在Direct3D9 和Direct3D10 API上的一个程序框架.它的目标是建立一个强大而容易使用的D ...
- I.MX6 KEY_ROW4 can't as GPIO pin
/********************************************************************** * I.MX6 KEY_ROW4 can't as GP ...
- vs2012 + cocos2d-x 2.1.5 + win7开发环境搭建步骤
先要让vs具备cocos2d-x项目的模板,以此可以创建新的项目(1-5步),然后把相关的源码库文件和动态连接库都拷贝到自己的项目中,以使项目可以正常运行(6-7步). 1,打开vs,设置TestCp ...
- 堆(Heap)和二叉堆(Binary heap)
堆(Heap) The operations commonly performed with a heap are: create-heap: create an empty heap heapify ...
- ubuntu基本配置
新系统装好后的操作: 1.resource updata:服务器镜像地址选择 2.删除不必要软件: 2.1:libreoffice sudo apt-get remove libreoffice-co ...
- asp.net网站 提示Ambiguous match found
在ASP.net中,每个aspx页面都会有一个.cs文件,(好像不可以多个aspx共用一个cs的,我前面就碰到这个问题), 在aspx页面中,我们会用到服务器控件,或html控件,这些控件的id命名时 ...
- 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...