关于python mysql的问题

关于python return 的一点小问题_python吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:62,611贴子:
关于python return 的一点小问题收藏
为什么在输入错误后重新输入,返回的值是none呢如果第一次就输入正确的数则返回正常值
万恶的logo,左下角是x = input_num()print x
你在if XXX: input_num() 后面加上return (随意), 再运行看看 结果是貌似函数返回的是第一次调用函数返回的值,因为你第一次输入大於上限,第一次调用返回的是None, 所以又第二次调用了这个函数,第二次返回的是正常的数。具体什麽原理 不懂,也可能说错了,误人子弟就罪过大了。。。反正大概就是这麼一回事。。。。
你要是学过其他编程语言就知道了
你这是递归没做好判断的问题:在代码里添加点提示性的问题,就一目了然了:-----------------修改后----------------------#!/usr/bin/env python def input_num():
num = input('come on,
give me sth:')
if num & 10000 or num & 99999:
input_num()
print 'current position: in function'
return numx = input_num()print 'current position: mainEntrance'print x-------------------Output--------------------D:\Program Files\HA_notepad3.7&python check.pycome on,
give me sth:1234come on,
give me sth:12345current position: in function12345current position: mainEntranceNone
另一个问题def input_num():
while True:
num = input('come on, give me sth:')
if num & 10000 or num & 99999:pass
print 'current position: in function'
return numx = input_num()print 'current position: mainEntrance'print x-------------------------------------------1.为什么return num后input_num函数就自己跳出循环了不需要break跳出2.这种判断输入内容不符合条件就重新输入的函数如何写比较好
1. 在def里 return会直接退出任何东西 然后返回给function的调用者2. 我一般用这种写法def input_num(): while True:  n = input()  if 10000 & num & 99999:    return n
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或Python程序员开发中常犯的10个错误
投稿:junjie
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Python程序员开发中常犯的10个错误,不知道你有没有中枪呢,需要的朋友可以参考下
Python是一门简单易学的编程语言,语法简洁而清晰,并且拥有丰富和强大的类库。与其它大多数程序设计语言使用大括号不一样 ,它使用缩进来定义语句块。
  在平时的工作中,Python开发者很容易犯一些小错误,这些错误都很容易避免,本文总结了Python开发者最常犯的10个错误,一起来看下,不知你中枪了没有。
1.滥用表达式作为函数参数默认值
  Python允许开发者指定一个默认值给函数参数,虽然这是该语言的一个特征,但当参数可变时,很容易导致混乱,例如,下面这段函数定义:
&&& def foo(bar=[]):&&&&&&& # bar is optional and defaults to [] if not specified
...&&& bar.append("baz")&&& # but this line could be problematic, as we'll see...
...&&& return bar
  在上面这段代码里,一旦重复调用foo()函数(没有指定一个bar参数),那么将一直返回'bar',因为没有指定参数,那么foo()每次被调用的时候,都会赋予[]。下面来看看,这样做的结果:
["baz", "baz"]
["baz", "baz", "baz"]
解决方案:
&&& def foo(bar=None):
...&&& if bar is None:&&# or if not bar:
...&&&&&&& bar = []
...&&& bar.append("baz")
...&&& return bar
2.错误地使用类变量
先看下面这个例子:
&&& class A(object):
...&&&& x = 1
&&& class B(A):
...&&&& pass
&&& class C(A):
...&&&& pass
&&& print A.x, B.x, C.x
这样是有意义的:
&&& B.x = 2
&&& print A.x, B.x, C.x
再来一遍:
&&& A.x = 3
&&& print A.x, B.x, C.x
  仅仅是改变了A.x,为什么C.x也跟着改变了。
  在Python中,类变量都是作为字典进行内部处理的,并且遵循方法解析顺序(MRO)。在上面这段代码中,因为属性x没有在类C中发现,它会查找它的基类(在上面例子中只有A,尽管Python支持多继承)。换句话说,就是C自己没有x属性,独立于A,因此,引用 C.x其实就是引用A.x。
3.为异常指定不正确的参数
  假设代码中有如下代码:
...&&&& l = ["a", "b"]
...&&&& int(l[2])
... except ValueError, IndexError:& # To catch both exceptions, right?
...&&&& pass
Traceback (most recent call last):
& File "&stdin&", line 3, in &module&
IndexError: list index out of range
  问题在这里,except语句并不需要这种方式来指定异常列表。然而,在Python 2.x中,except Exception,e通常是用来绑定异常里的 第二参数,好让其进行更进一步的检查。因此,在上面这段代码里,IndexError异常并没有被except语句捕获,异常最后被绑定 到了一个名叫IndexError的参数上。
  在一个异常语句里捕获多个异常的正确方法是指定第一个参数作为一个元组,该元组包含所有被捕获的异常。与此同时,使用as关键字来保证最大的可移植性,Python 2和Python 3都支持该语法。
...&&&& l = ["a", "b"]
...&&&& int(l[2])
... except (ValueError, IndexError) as e:&
...&&&& pass
4.误解Python规则范围
  Python的作用域解析是基于LEGB规则,分别是Local、Enclosing、Global、Built-in。实际上,这种解析方法也有一些玄机,看下面这个例子:
&&& x = 10
&&& def foo():
...&&&& x += 1
...&&&& print x
Traceback (most recent call last):
& File "&stdin&", line 1, in &module&
& File "&stdin&", line 2, in foo
UnboundLocalError: local variable 'x' referenced before assignment
许多人会感动惊讶,当他们在工作的函数体里添加一个参数语句,会在先前工作的代码里报UnboundLocalError错误( 查看更详细描述)。
在使用列表时,开发者是很容易犯这种错误的,看看下面这个例子:
&&& lst = [1, 2, 3]
&&& def foo1():
...&&&& lst.append(5)&& # This works ok...
&&& foo1()
[1, 2, 3, 5]
&&& lst = [1, 2, 3]
&&& def foo2():
...&&&& lst += [5]&&&&& # ... but this bombs!
&&& foo2()
Traceback (most recent call last):
& File "&stdin&", line 1, in &module&
& File "&stdin&", line 2, in foo
UnboundLocalError: local variable 'lst' referenced before assignment
为什么foo2失败而foo1运行正常?
  答案与前面那个例子是一样的,但又有一些微妙之处。foo1没有赋值给lst,而foo2赋值了。lst += [5]实际上就是lst = lst + [5],试图给lst赋值(因此,假设Python是在局部作用域里)。然而,我们正在寻找指定给lst的值是基于lst本身,其实尚未确定。
5.修改遍历列表
  下面这段代码很明显是错误的:
&&& odd = lambda x : bool(x % 2)
&&& numbers = [n for n in range(10)]
&&& for i in range(len(numbers)):
...&&&& if odd(numbers[i]):
...&&&&&&&& del numbers[i]& # BAD: Deleting item from a list while iterating over it
Traceback (most recent call last):
& && File "&stdin&", line 2, in &module&
IndexError: list index out of range
在遍历的时候,对列表进行删除操作,这是很低级的错误。稍微有点经验的人都不会犯。
  对上面的代码进行修改,正确地执行:
&&& odd = lambda x : bool(x % 2)
&&& numbers = [n for n in range(10)]
&&& numbers[:] = [n for n in numbers if not odd(n)]& # ahh, the beauty of it all
&&& numbers
[0, 2, 4, 6, 8]
6.如何在闭包中绑定变量
  看下面这个例子:
&&& def create_multipliers():
...&&&& return [lambda x : i * x for i in range(5)]
&&& for multiplier in create_multipliers():
...&&&& print multiplier(2)
  你期望的结果是:
  实际上:
 是不是非常吃惊!出现这种情况主要是因为Python的后期绑定行为,该变量在闭包中使用的同时,内部函数又在调用它。
  解决方案:
&&& def create_multipliers():
...&&&& return [lambda x, i=i : i * x for i in range(5)]
&&& for multiplier in create_multipliers():
...&&&& print multiplier(2)
7.创建循环模块依赖关系
  假设有两个文件,a.py和b.py,然后各自导入,如下:
  在a.py中:
&&& return b.x
  在b.py中:
&&& print a.f()
首先,让我们试着导入a.py:
代码如下:&&& import a
  可以很好地工作,也许你会感到惊讶。毕竟,我们确实在这里做了一个循环导入,难道不应该有点问题吗?
  仅仅存在一个循环导入并不是Python本身问题,如果一个模块被导入,Python就不会试图重新导入。根据这一点,每个模块在试图访问函数或变量时,可能会在运行时遇到些问题。
  当我们试图导入b.py会发生什么(先前没有导入a.py):
&&& import b
Traceback (most recent call last):
& && File "&stdin&", line 1, in &module&
& && File "b.py", line 1, in &module&
&&& import a
& && File "a.py", line 6, in &module&
&print f()
& && File "a.py", line 4, in f
&return b.x
AttributeError: 'module' object has no attribute 'x'
 出错了,这里的问题是,在导入b.py的过程中还要试图导入a.py,这样就要调用f(),并且试图访问b.x。但是b.x并未被定义。
  可以这样解决,仅仅修改b.py导入到a.py中的g()函数:
&&& import a&# This will be evaluated only when g() is called
&&& print a.f()
无论何时导入,一切都可以正常运行:
&&& import b
1&# Printed a first time since module 'a' calls 'print f()' at the end
1&# Printed a second time, this one is our call to 'g'
8.与Python标准库模块名称冲突
  Python拥有非常丰富的模块库,并且支持“开箱即用”。因此,如果不刻意避免,很容易发生命名冲突事件。例如,在你的代码中可能有一个email.py的模块,由于名称一致,它很有可能与Python自带的标准库模块发生冲突。
9.未按规定处理Python2.x和Python3.x之间的区别
  看一下foo.py:
import sys
def bar(i):
&&& if i == 1:
&&&&&&& raise KeyError(1)
&&& if i == 2:
&&&&&&& raise ValueError(2)
def bad():
&&& e = None
&&&&&&& bar(int(sys.argv[1]))
&&& except KeyError as e:
&&&&&&& print('key error')
&&& except ValueError as e:
&&&&&&& print('value error')
&&& print(e)
在Python 2里面可以很好地运行:
$ python foo.py 1
$ python foo.py 2
value error
  但是在Python 3里:
$ python3 foo.py 1
Traceback (most recent call last):
& File "foo.py", line 19, in &module&
& File "foo.py", line 17, in bad
&&& print(e)
UnboundLocalError: local variable 'e' referenced before assignment
解决方案:
import sys
def bar(i):
&&& if i == 1:
&&&&&&& raise KeyError(1)
&&& if i == 2:
&&&&&&& raise ValueError(2)
def good():
&&& exception = None
&&&&&&& bar(int(sys.argv[1]))
&&& except KeyError as e:
&&&&&&& exception = e
&&&&&&& print('key error')
&&& except ValueError as e:
&&&&&&& exception = e
&&&&&&& print('value error')
&&& print(exception)
 在Py3k中运行结果:
代码如下:$ python3 foo.py 1
$ python3 foo.py 2
value error
  在 Python招聘指南里有许多关于Python 2与Python 3在移植代码时需要关注的注意事项与讨论,大家可以前往看看。
10.滥用__del__方法
  比如这里有一个叫mod.py的文件:
import foo
class Bar(object):
&& &&&& ...
&&& def __del__(self):
&&&&&&& foo.cleanup(self.myhandle)
  下面,你在another_mod.py文件里执行如下操作:
import mod
mybar = mod.Bar()
 你会获得一个AttributeError异常。
  至于为什么会出现该异常,点击这里查看详情。当解释器关闭时,该模块的全局变量全部设置为None。因此,在上面这个例子里,当__del__被调用时,foo已经全部被设置为None。
  一个很好的解决办法是使用atexit.register()代替。顺便说一句,当程序执行完成后,您注册的处理程序会在解释器关闭之前停止 工作。
  修复上面问题的代码:
import foo
import atexit
def cleanup(handle):
&&& foo.cleanup(handle)
class Bar(object):
&&& def __init__(self):
&&&&&&& ...
&&&&&&& atexit.register(cleanup, self.myhandle)
在程序的正常终止的前提下,这个实现提供了一个整洁可靠的方式调用任何需要清理的功能。
  Python是一款强大而灵活的编程语言,并且带有许多机制和模式来大大提高工作效率。正如任何一门语言或软件工具一样,人们对其能力都会存在一个限制性地理解或欣赏,有些是弊大于利,有些时候反而会带来一些陷进。 体会一名语言的细微之处,理解一些常见的陷阱,有助于你在开发者的道路上走的更远。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具问大神一个问题,关于 python 和 anaconda 的 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
已注册用户请 &
推荐学习书目
Python Sites
值得关注的项目
Python 编程
Sponsored by
国内领先的实时后端云野狗 API 可用于开发即时聊天、网络游戏、实时定位等实时场景传输快!响应快!入门快!
Promoted by
问大神一个问题,关于 python 和 anaconda 的
23:18:55 +08:00 · 8662 次点击
问大神一个问题,关于python和anaconda的,我在mac上安装了python和anaconda,现在anaconda目录下有python2.7,我现在每次安装其他的包,每次都安装到anaconda目录下的python中去,怎么解决这个问题?把安装的包安装到python的目录。安装的包比如:scrapy,谢谢!
第 1 条附言 &·&
23:00:46 +08:00
以上方法还是没能解决我的问题,现在mac下的anaconda怎么卸载?有没有人知道?
15 回复 &| &直到
15:07:41 +08:00
& & 23:32:20 +08:00
。。。。同关注,因为numpy装了个anaconda,感觉很坑爹
& & 00:00:06 +08:00
那你不如单独安装需要的packages就行。
& & 00:45:33 +08:00
最好用virtualenv,省的折腾
& & 00:55:16 +08:00
```
brew install python
brew install pyenv
pyenv install anaconda-2.1.0
```
Use brew python
```
pip install &package&
python ...
```
Use anaconda
```
pyenv shell anaconda-2.1.0
pip install &package&
python ...
```
& & 07:47:19 +08:00
没有看懂,你是说想将包安装到系统自带的Python环境中吗?
& & 11:39:30 +08:00 via Android
anaconda 不好吗?我默认的就用它,感觉不错啊。
& & 17:14:27 +08:00
@ 嗯,对的,但是默认的是没有安装到系统的python中去
& & 17:14:50 +08:00
@ 使用得不多,可能还是想直接在python目录使用吧
& & 17:15:21 +08:00
@ 是啊,现在想来着,但是mac已经安装了anaconda
& & 17:15:36 +08:00
@ 好的,我先看看,谢谢
& & 17:16:23 +08:00
@ 好,我先试试
& & 18:16:36 +08:00
anaconda会修改.bashrc之类的配置文件,你编辑一下,把它添加的路径配置注释掉就好了。
& & 21:10:44 +08:00
@ 嗯,尝试一下,谢谢!
& & 11:54:35 +08:00
如果楼主做python+cuda开发,那么anaconda不错。
如果单纯为了科学开发,那么numpy+scipy+pandas+matplotlib+opencv足够了。
另外,建议楼主以后多使用vitualenv。
& & 15:07:41 +08:00
@ 好的,谢谢!
& · & 463 人在线 & 最高记录 1893 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.7.3 · 46ms · UTC 01:26 · PVG 09:26 · LAX 18:26 · JFK 21:26? Do have faith in what you're doing.python中一些常见的问题解决汇总
PEP 0263 -- Defining Python Source Code Encodings
来源:点击进入
转载日期:
Defining Python Source Code Encodings
Last-Modified:
22:03:18 +0200 (Thu, 28 Jun 2007)
Marc-André Lemburg &mal &, Martin von L?wis &martin at v.loewis.de&
Standards Track
06-Jun-2001
Python-Version:
Post-History:
This PEP proposes to introduce a syntax to declare the encoding of
a Python source file. The encoding information is then used by the
Python parser to interpret the file using the given encoding. Most
notably this enhances the interpretation of Unicode literals in
the source code and makes it possible to write Unicode literals
using e.g. UTF-8 directly in an Unicode aware editor.Problem
In Python 2.1, Unicode literals can only be written using the
Latin-1 based encoding "unicode-escape". This makes the
programming environment rather unfriendly to Python users who live
and work in non-Latin-1 locales such as many of the Asian
countries. Programmers can write their 8-bit strings using the
favorite encoding, but are bound to the "unicode-escape" encoding
for Unicode literals.Proposed Solution
I propose to make the Python source code encoding both visible and
changeable on a per-source file basis by using a special comment
at the top of the file to declare the encoding.
To make Python aware of this encoding declaration a number of
concept changes are necessary with respect to the handling of
Python source code data.Defining the Encoding
Python will default to ASCII as standard encoding if no other
encoding hints are given.
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:
# coding=&encoding name&
or (using formats recognized by popular editors)
#!/usr/bin/python
# -*- coding: &encoding name& -*-
#!/usr/bin/python
# vim: set fileencoding=&encoding name& :
More precisely, the first or second line must match the regular
expression "coding[:=]\s*([-\w.]+)". The first group of this
expression is then interpreted as encoding name. If the encoding
is unknown to Python, an error is raised during compilation. There
must not be any Python statement on the line that contains the
encoding declaration.
To aid with platforms such as Windows, which add Unicode BOM marks
to the beginning of Unicode files, the UTF-8 signature
'\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well
(even if no magic encoding comment is given).
If a source file uses both the UTF-8 BOM mark signature and a
magic encoding comment, the only allowed encoding for the comment
is 'utf-8'.
Any other encoding will cause an error.Examples
These are some examples to clarify the different styles for
defining the source code encoding at the top of a Python source
1. With interpreter binary and using Emacs style file encoding
#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import os, sys
#!/usr/bin/python
# -*- coding: ascii -*-
import os, sys
2. Without interpreter line, using plain text:
# This Python file uses the following encoding: utf-8
import os, sys
3. Text editors might have different ways of defining the file's
encoding, e.g.
#!/usr/local/bin/python
# coding: latin-1
import os, sys
4. Without encoding comment, Python's parser will assume ASCII
#!/usr/local/bin/python
import os, sys
5. Encoding comments which don't work:
Missing "coding:" prefix:
#!/usr/local/bin/python
import os, sys
Encoding comment not on line 1 or 2:
#!/usr/local/bin/python
# -*- coding: latin-1 -*-
import os, sys
Unsupported encoding:
#!/usr/local/bin/python
# -*- coding: utf-42 -*-
import os, sys
...Concepts
The PEP is based on the following concepts which would have to be
implemented to enable usage of such a magic comment:
1. The complete Python source file should use a single encoding.
Embedding of differently encoded data is not allowed and will
result in a decoding error during compilation of the Python
source code.
Any encoding which allows processing the first two lines in the
way indicated above is allowed as source code encoding, this
includes ASCII compatible encodings as well as certain
multi-byte encodings such as Shift_JIS. It does not include
encodings which use two or more bytes for all characters like
e.g. UTF-16. The reason for this is to keep the encoding
detection algorithm in the tokenizer simple.
2. Handling of escape sequences should continue to work as it does
now, but with all possible source code encodings, that is
standard string literals (both 8-bit and Unicode) are subject to
escape sequence expansion while raw string literals only expand
a very small subset of escape sequences.
3. Python's tokenizer/compiler combo will need to be updated to
work as follows:
1. read the file
2. decode it into Unicode assuming a fixed per-file encoding
3. convert it into a UTF-8 byte string
4. tokenize the UTF-8 content
5. compile it, creating Unicode objects from the given Unicode data
and creating string objects from the Unicode literal data
by first reencoding the UTF-8 data into 8-bit string data
using the given file encoding
Note that Python identifiers are restricted to the ASCII
subset of the encoding, and thus need no further conversion
after step 4.Implementation
For backwards-compatibility with existing code which currently
uses non-ASCII in string literals without declaring an encoding,
the implementation will be introduced in two phases:
1. Allow non-ASCII in string literals and comments, by internally
treating a missing encoding declaration as a declaration of
"iso-8859-1". This will cause arbitrary byte strings to
correctly round-trip between step 2 and step 5 of the
processing, and provide compatibility with Python 2.2 for
Unicode literals that contain non-ASCII bytes.
A warning will be issued if non-ASCII bytes are found in the
input, once per improperly encoded input file.
2. Remove the warning, and change the default encoding to "ascii".
The builtin compile() API will be enhanced to accept Unicode as
input. 8-bit string input is subject to the standard procedure for
encoding detection as described above.
If a Unicode string with a coding declaration is passed to compile(),
a SyntaxError will be raised.
SUZUKI Hisao i see [2] for details. A patch
implementing only phase 1 is available at [1].Phases
Implemenation of steps 1 and 2 above were completed in 2.3,
except for changing the default encoding to "ascii".
The default encoding was set to "ascii" in version 2.5.
This PEP intends to provide an upgrade path from the current
(more-or-less) undefined source code encoding situation to a more
robust and portable definition.References
[1] Phase 1 implementation:
http://python.org/sf/526840
[2] Phase 2 implementation:
http://python.org/sf/534304History
1.10 and above: see CVS history
1.8: Added '.' to the coding RE.
1.7: Added warnings to phase 1 implementation. Replaced the
Latin-1 default encoding with the interpreter's default
encoding. Added tweaks to compile().
1.4 - 1.6: Minor tweaks
1.3: Worked in comments by Martin v. Loewis:
UTF-8 BOM mark detection, Emacs style magic comment,
two phase approach to the implementationCopyright
This document has been placed in the public domain.
chinakr 发短消息加为好友chinakr
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 .
传说中的长老
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 . 12#
00:18 | 只看该作者 Python中的utf-8编码与gb2312编码
来源:点击进入
作者:Yoker 日期:
  上次在某个文章中看到,只要把你写好的xml文件用记事本保存的时候选择utf-8编码,如果xml内容本身也采用的是utf-8编码,可以直接访问,是没有问题的。写好的代码如下:
&?xml version="1.0" encoding="utf-8" ?&
&rss version="2.0"&
&title&&![CDATA[rss标题文字]]&&/title&
&link&/&/link&
&description&
&![CDATA[文字...]]&
&/description&
&copyright&&![CDATA[Copyright (c) . All Rights Reserved,Inc.]]&&/copyright&
&webMaster&yoker.&/webMaster&
&pubDate& 18:14:28&/pubDate&
&title&&![CDATA[文字标题]]&&/title&
&link&/News/69.html&/link&
&description&&![CDATA[详细文字内容]]&&/description&
&pubDate& 10:29:12&/pubDate&
&/channel&
  当时看了文章,也没有太在意,恍然大捂的那种心情(自己老是这样粗心)。今天在改写一个网站的RSS的时候,发现了问题。我把程序rss.asp写好,用utf-8方式保存程序文件,放到服务器测试,问题就来了,由于是动态文件生成的xml格式数据,所以报了一个找不到服务器的页面错误给我,一头雾水!直觉告诉我是xml格式有错,但错在什么地方呢?这样一个错误页面,叫我如何思考?
  随后,我用fso把动态调用的数据加上xml格式,生成一个xml文件rss.xml丢到相同目录,访问生成后的rss.xml文件,乱码!再次陷入僵局。
  我打开文件rss.xml,看了一篇又一篇,乱了阵脚。跑到QQ群一篇狂呼,没有一人响应,是太简单了吗?还是我根本没有把问题阐述清楚呢?反正是没有人回答,上我最可爱的老师那里去找答案:,输入:asp utf-8 保存文件出来了一大堆。随便翻了几个网页,都有Adodb.Stream的东西,那些参数曾经认真去学习过,现在看到又觉得好陌生哦,再翻翻手册,思路慢慢打开,fso保存文件是按照Ansi编码,而我的xml格式数据是utf-8编码,难怪不得!
  回到了问题的根本上来了,是编码问题!我又瞢着头瞎折腾一翻后,未果。静下心思考,我程序用utf-8保存,而我再次用utf-8编码方式去生成的 rss.xml文件,这两次的utf-8一定有点问题吧,于是改写代码用ANSI保存程序文件rss.asp,再用utf-8编码方式保存调用后形成的 xml格式数据rss.xml,用FF访问rss.xml,OK,可爱的xml树结构出现了。才知道这和我最开始看到的文章有点关联了。那动态调用的文件 rss.asp为什么该怎么处理呢?又一个0.5秒的思考,增加Response.CharSet = "utf-8"访问,出来的是一堆地址列表。。而不是我想要的xml树,再看看源代码,乱码。这又是什么原因呐?
  心烦,都这么不顺利呀!静下心,我翻到了我的头部包含文件,看到一个让我今生难忘的代码:&%@ LANGUAGE = VBScript CodePage = 936%&,936 不就是gb2312嘛,狗东西,害得我好惨。但我还不能直接修改他,要不然其他页面我不是乱套了,于是又问了问,发现 session.codepage = "65001"还可以用,不过有过缺点,他会修改整个站的codepage,那不是和直接修改包含文件一样了,灵机一动,我用完后,又恢复不就得了。用上,完全成功!
结论:采用UTF-8编码,除了要将文件另存为UTF-8格式之外,还需要同时指定codepage及charset
附件:用utf-8保存Ansi数据代码如下:
Dim oStream
Set oStream = Server.CreateObject("ADODB.Stream")
With oStream
.Mode = 3 : .Type = 2 : .Charset = "utf-8" : .Open
.WriteText RssStr
.SaveToFile server.mappath("/Rss.xml"), 2
oStream = Nothing
chinakr 发短消息加为好友chinakr
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 .
传说中的长老
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 . 13#
00:19 | 只看该作者 python 的编码转换知识
来源:点击进入
作者:Yoker 日期:
  主要介绍了python的编码机制以及编码格式,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换。
一、python源码文件的编码
在源代码中如果包含了中文等汉字,就得对文件本身编码进行声明。这个编码同时决定了在该源文件中声明的字符串的编码格式。
#!/usr/bin/python
# -*- coding: utf-8 -*-
二、python读取外部文件的编码
外部文件的读取有:磁盘文件已经网络文件,这都涉及到编码的问题。如果读取外面的文件是gb2312编码格式的文件,那么在程序内部需要使用的时候,就必须转码才可以
def gethtml(url):
"""读取gb2312编码的文件,转换为utf8格式"""
import urllib
html = urllib.urlopen(url).read().decode('gb2312')
return html.encode('utf8')
三、python内部编码及转换
1、unicode 转换为其它编码(GBK, GB2312等)
例如:a为unicode编码 要转为gb2312。a.encode('gb2312')
# -*- coding=gb2312 -*-
a = u"中文"
a_gb2312 = a.encode('gb2312')
print a_gb2312
2、其它编码(utf-8,GBK)转换为unicode
例如:a为gb2312编码,要转为unicode. unicode(a, 'gb2312')或a.decode('gb2312')
# -*- coding=gb2312 -*-
a = u"中文"
a_gb2312 = a.encode('gb2312')
print a_gb2312
a_unicode = a_gb2312.decode('gb2312')
assert(a_unicode == a)
a_utf_8 = a_unicode.encode('utf-8')
print a_utf_8
3、非unicode编码之间的转换
编码1(GBK,GB2312)
转换为 编码2(utf-8,utf-16,ISO-8859-1) ,可以先转为unicode再转为编码2 ,如gb2312转utf-8
# -*- coding=gb2312 -*-
a = u"中文"
a_gb2312 = a.encode('gb2312')
print a_gb2312
a_unicode = a_gb2312.decode('gb2312')
assert(a_unicode == a)
a_utf_8 = a_unicode.encode('utf-8')
print a_utf_8
4、判断字符串的编码
isinstance(s, str) 用来判断是否为一般字符串;isinstance(s, unicode) 用来判断是否为unicode。如果一个字符串已经是unicode了,再执行unicode转换有时会出错(并不都出错)
下面代码为将任意字符串转换为unicode
def u(s, encoding):
if isinstance(s, unicode):
return unicode(s, encoding)
5、unicode 与其它编码之间的区别
为什么不所有的文件都使用unicode,还要用GBK,utf-8等编码呢?unicode可以称为抽象编码,也就是它只是一种内部表示,一般不能直接保存。保存到磁盘上时,需要把它转换为对应的编码,如utf-8和utf-16。
6、其它方法
除上以上的编码方法,在读写文件时还可以使用codecs的open方法在读写时进行转换。
相关阅读:
http://blog.csdn.net/kiki113/archive//4062063.aspx
http://blog.csdn.net/lanphaday/archive//2834883.aspx
/daping_zhang/blog/item/09dda71ea9d7d21f4134173e.html
http://www.python.org/dev/peps/pep-0008/ *
http://boodebr.org/main/python/all-about-python-and-unicode *
chinakr 发短消息加为好友chinakr
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 .
传说中的长老
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 . 14#
12:39 | 只看该作者 python与中文字体
google关键字:python Bad Unicode data
来源:点击进入
cmsgoogle 发表于
python读入和显示中文字体有些问题,网上搜了搜,发现下面这个链接还是给出了解决方法
主要是引入一个codecs的模块。
/articles/unicode/python.html
*********************************************************************
原文内容:
Unicode in Python
The first thing to know about Python's Unicode support is that you may need to install a recent version of Python to get it. Users of RedHat Linux 7.x have Python 1.5.2 by default, for compatibility reasons. Unicode support was introduced in Python 1.6.
Unicode Strings in Python
Python has two different string types: an 8-bit non-Unicode string type (str) and a 16-bit Unicode string type (unicode).
Unicode strings are written with a leading u. They may contain Unicode escape sequences of the form \u0000, just as in Java. For example:
question = u'\u00bfHabla espa\u00f1ol?'
# ?Habla espa?ol?Some Unicode characters have numbers beyond U+FFFF, so Python has another escape: \U, which offers more than enough digits to specify any Unicode codepoint. (Recent C and C++ standards also offer this, but Java does not.)
Python also offers a \N escape which allows you to specify any Unicode character by name.
# This string has 7 characters in all, including the spaces
# between the symbols.
symbols = u'\N{BLACK STAR} \N{WHITE STAR} \N{LIGHTNING} \N{COMET}'One more way to build a Unicode string object is with the built-in unichr() function, which is the Unicode version of chr().
Unicode Support in the Python Standard Library
Unicode strings are very similar to Python's ordinary 8-bit strings. They have the same useful methods (split(), strip(), find(), and so on). The + and * operators work on Unicode strings just as they do for plain strings. And like plain strings, Unicode strings can do printf-like formatting, using the % symbol. For the most part, you'll feel right at home.
This seamlessness extends to most of Python's standard library.
?Python regular expressions can search Unicode strings.
?Python's standard gettext module supports Unicode. This is the module to use for internationalization of Python programs.
?The Tkinter GUI toolkit offers excellent Unicode support. Here is a minimal Hello, world program using Unicode and Tkinter.
?Python's standard XML library is Unicode-aware (as required by the XML specification).
Most of the standard library works smoothly with Unicode strings. Some modules still are not fully Unicode-friendly, but the most important pieces are in place.
Unicode files and Python
Reading and writing Unicode files from Python is simple. Use codecs.open() and specify the encoding.
import codecs
# Open a UTF-8 file in read mode
infile = codecs.open("infile.txt", "r", "utf-8")
# Read its contents as one large Unicode string.
text = infile.read()
# Close the file.
infile.close()The same function is used to ope just use "w" (write) or "a" (append) as the second argument.
A fourth argument, after the encoding, can be provided to specify error-handling. The possible values are:
?'strict' - The default. Throw exceptions if errors are detected while encoding or decoding data.
?'ignore' - Skip over errors or unencodeable characters.
?'replace' - Replace bad or unencodeable data with a "replacement character", usually a question mark.
Since 'strict' is the default, expect a lot of UnicodeExceptions to be thrown if your data isn't quite right. Once you get the hang of it, those errors become much less frequent.
Sometimes a program simply needs to encode or decode a single chunk of Unicode data. This, too, is easy in Python: Unicode strings have an encode() method that returns a str, and str objects have a decode() method that returns a unicode string.
# Suppose we are given these bytes, perhaps over a socket
# or perhaps taken from a database.
bytes = 'Bun\xc4\x83-diminea\xc8\x9ba, lume'
# We want to convert these UTF-8 bytes to a Unicode string.
unicode_strg = bytes.decode('utf-8')
# Now print it, but in the ISO-8859-1 encoding, because
# (let's suppose) that is the format of our display.
print unicode_strg.encode('iso-8859-1', 'replace')However, note that in this particular example, the source string contains two characters (? and ?) that are not available in ISO-8859-1! Unfortunately, if our display can only handle ISO-8859-1 characters, there is no satisfactory answer to this problem. Some characters will be lost. The last line of the sample code instructs Python to use the 'replace' error-handling behavior instead of the default 'strict' behavior. This way, although some characters will be replaced with question marks, at least no exception will be thrown.
Of course, it would be better to use a display that can handle all Unicode characters, such as a Tk GUI.
print and Unicode strings
We now come to the most puzzling aspect of Python's Unicode support. Attempting to print a Unicode string causes an error:
&&& print u'\N{POUND SIGN}'Traceback (most recent call last):
File "&stdin&", line 1, in ?UnicodeError: ASCII encoding error: ordinal not in range(128) Two elements combine to cause this error:
1.Python's default encoding is ASCII. The pound sign is not an ASCII character. (By contrast, Java's default encoding is usually something like Latin-1, which covers a bit more ground than ASCII.)
2.The default error behavior is 'strict'. If Python encounters a character that it can't encode, it raises a UnicodeError. (This is different from Java, which silently replaces the character with a ? instead.)
Python defaults to ASCII because ASCII is the only thing likely to work everywhere. The correct encoding is not always Latin-1. In fact, it depends on how you are accessing Python.
When Python executes a print statement, it simply passes the output to the operating system (using fwrite() or something like it), and some other program is responsible for actually displaying that output on the screen. For example, on Windows, it might be the Windows console subsystem that displays the result. Or if you're using Windows and running Python on a Unix box somewhere else, your Windows SSH client is actually responsible for displaying the data. If you are running Python in an xterm on Unix, then xterm and your X server handle the display.
To print data reliably, you must know the encoding that this display program expects.
Earlier it was mentioned that IBM PC computers use the "IBM Code Page 437" character set at the BIOS level. The Windows console still emulates CP437. So this print statement will work, on Windows, under a console window.
# Windows console mode only
&&& s = u'\N{POUND SIGN}'
&&& print s.encode('cp-437')
?Several SSH clients display data using the Latin-1 Tkinter assumes UTF-8, when 8-bit strings are passed into it. So in general it is not possible to determine what encoding to use with print. It is therefore better to send Unicode output to files or Unicode-aware GUIs, not to sys.stdout.
*********************************************************************
一段用于读写的代码:
import ocdecs as p
infile = p.open('inputfile','r','utf-8')-------------the third parar must satisfy the inputfile's original format
outfile = p.open('outputfile','w','utf-8')-----------the third parar is chosen by yourself,any one u like:
while True:
line = infile.readline()
outfile.write(line)
line----------------now it can be displayed
normally, not messy code
outfile.close()
infile.close()
chinakr 发短消息加为好友chinakr
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 .
传说中的长老
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 . 15#
12:52 | 只看该作者 print 中文处理
google关键字:python脚本 默认codec
来源:点击进入
来源: http://blog.chinaunix.net/u2/68206/showart_668359.html
转载:Python、Unicode和中文
python的中文问题一直是困扰新手的头疼问题,这篇文章将给你详细地讲解一下这方面的知识。当然,几乎可以确定的是,在将来的版本中,python会彻底解决此问题,不用我们这么麻烦了。
先来看看python的版本:
&&& import sys
&&& sys.version
'2.5.1 (r251:54863, Apr 18 :08) [MSC v.1310 32 bit (Intel)]'
用记事本创建一个文件ChineseTest.py,默认ANSI:
s = "中文"
测试一下瞧瞧:
E:\Project\Python\Test&python ChineseTest.py
File "ChineseTest.py", line 1
SyntaxError: Non-ASCII character '\xd6' in file ChineseTest.py on line 1, but
see http://www.pytho
n.org/peps/pep-0263.html for details
偷偷地把文件编码改成UTF-8:
E:\Project\Python\Test&python ChineseTest.py
File "ChineseTest.py", line 1
SyntaxError: Non-ASCII character '\xe4' in file ChineseTest.py on line 1, but
see http://www.pytho
n.org/peps/pep-0263.html for details
无济于事。。。
既然它提供了网址,那就看看吧。简单地浏览一下,终于知道如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明。把ChineseTest.py文件的编码重新改为ANSI,并加上编码声明:
# coding=gbk
s = "中文"
再试一下:
E:\Project\Python\Test&python ChineseTest.py
正常咯:)
看一看它的长度:
# coding=gbk
s = "中文"
print len(s)
s这里是str类型,所以计算的时候一个中文相当于两个英文字符,因此长度为4。
我们这样写:
# coding=gbk
s = "中文"
s1 = u"中文"
s2 = unicode(s, "gbk") #省略参数将用python默认的ASCII来解码
s3 = s.decode("gbk") #把str转换成unicode是decode,unicode函数作用与之相同
print len(s1)
print len(s2)
print len(s3)
接着来看看文件的处理:
建立一个文件test.txt,文件格式用ANSI,内容为:
用python来读取
# coding=gbk
print open("Test.txt").read()
结果:abc中文
把文件格式改成UTF-8:
结果:abc涓?枃
显然,这里需要解码:
# coding=gbk
import codecs
print open("Test.txt").read().decode("utf-8")
结果:abc中文
上面的test.txt我是用Editplus来编辑的,但当我用Windows自带的记事本编辑并存成UTF-8格式时,
运行时报错:
Traceback (most recent call last):
File "ChineseTest.py", line 3, in &module&
print open("Test.txt").read().decode("utf-8")
UnicodeEncodeError: 'gbk' codec can't encode character u'\ufeff' in position 0: illegal multibyte sequence
原来,某些软件,如notepad,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM)。
因此我们在读取时需要自己去掉这些字符,python中的codecs module定义了这个常量:
# coding=gbk
import codecs
data = open("Test.txt").read()
if data[:3] == codecs.BOM_UTF8:
data = data[3:]
print data.decode("utf-8")
结果:abc中文
(四)一点遗留问题
在第二部分中,我们用unicode函数和decode方法把str转换成unicode。为什么这两个函数的参数用"gbk"呢?
第一反应是我们的编码声明里用了gbk(# coding=gbk),但真是这样?
修改一下源文件:
# coding=utf-8
s = "中文"
print unicode(s, "utf-8")
运行,报错:
Traceback (most recent call last):
File "ChineseTest.py", line 3, in &module&
s = unicode(s, "utf-8")
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data
显然,如果前面正常是因为两边都使用了gbk,那么这里我保持了两边utf-8一致,也应该正常,不至于报错。
更进一步的例子,如果我们这里转换仍然用gbk:
# coding=utf-8
s = "中文"
print unicode(s, "gbk")
结果:中文
翻阅了一篇英文资料,它大致讲解了python中的print原理:
When Python executes a print statement, it simply passes the output to the operating system (using fwrite() or something like it), and some other program is responsible for actually displaying that output on the screen. For example, on Windows, it might be the Windows console subsystem that displays the result. Or if you're using Windows and running Python on a Unix box somewhere else, your Windows SSH client is actually responsible for displaying the data. If you are running Python in an xterm on Unix, then xterm and your X server handle the display.
To print data reliably, you must know the encoding that this display program expects.
简单地说,python中的print直接把字符串传递给操作系统,所以你需要把str解码成与操作系统一致的格式。Windows使用CP936(几乎与gbk相同),所以这里可以使用gbk。
最后测试:
# coding=utf-8
s = "中文"
print unicode(s, "cp936")
结果:中文
转载:Python 编码问题整理
几个概念性的东西
标准的 ANSCII 编码只使用7个比特来表示一个字符,因此最多编码128个字符。扩充的 ANSCII 使用8个比特来表示一个字符,最多也只能
编码 256 个字符。
使用2个甚至4个字节来编码一个字符,因此可以将世界上所有的字符进行统一编码。
UNICODE编码转换格式,就是用来指导如何将 unicode 编码成适合文件存储和网络传输的字节序列的形式 (unicode -&
str)。像其他的一些编码方式 gb2312, gb18030, big5 和 UTF 的作用是一样的,只是编码方式不同。
Python 里面有两种数据模型来支持字符串这种数据类型,一种是 str,另外一种是 unicode ,它们都是 sequence 的派生类
型,这个可以参考 Python Language Ref 中的描述:
The items of a string are characters. There is no separate
a character is represented by a string of one item.
Characters represent (at least) 8-bit bytes. The built-in functions
chr() and ord() convert between characters and nonnegative integers
representing the byte values. Bytes with the values 0-127 usually
represent the corresponding ASCII values, but the interpretation of
values is up to the program. The string data type is also used to
represent arrays of bytes, e.g., to hold data read from a file.
(On systems whose native character set is not ASCII, strings
may use EBCDIC in their internal representation, provided the
functions chr() and ord() implement a mapping between ASCII and
EBCDIC, and string comparison preserves the ASCII order. Or perhaps
someone can propose a better rule?)
The items of a Unicode object are Unicode code units. A
Unicode code unit is represented by a Unicode object of one item and
can hold either a 16-bit or 32-bit value representing a Unicode
ordinal (the maximum value for the ordinal is given in sys.maxunicode,
and depends on how Python is configured at compile time). Surrogate
pairs may be present in the Unicode object, and will be reported as
two separate items. The built-in functions unichr() and ord() convert
between code units and nonnegative integers representing the Unicode
ordinals as defined in the Unicode Standard 3.0. Conversion from and
to other encodings are possible through the Unicode method encode()
and the built-in function unicode().
这里面是这么几句:
"The items of a string are characters", "The items of a Unicode object
are Unicode code units", "The string data type is also used to
represent arrays of bytes, e.g., to hold data read from a file."
一二句说明 str 和 unicode 的组成单元(item)是什么(因为它们同是 sequence ) 。sequence 默认的
__len__ 函数的返回值正是该序列组成单元的个数。这样的话,len('abcd') == 4 和 len(u'我是中文') == 4 就很
容易理解了。
第三句告诉我们像从文件输入输出的时候是用 str 来表示数据的数组。不止是文件操作,我想在网络传输的时候应该也是这样的。这就是为什么一个
unicode 字符串在写入文件或者在网络上传输的时候要进行编码的原因了。
Python 里面的编码和解码也就是 unicode 和 str 这两种形式的相互转化。编码是 unicode -& str,相反的,解码就
是 str -& unicode。
下面剩下的问题就是确定何时需要进行编码或者解码了,像一些库是 unicode 版的,这样我们在将这些库函数的返回值进行传输或者写入文件的时候就
要考虑将它编码成合适的类型。
关于文件开头的"编码指示",也就是 # -*- coding: -*- 这个语句。Python 默认脚本文件都是 ANSCII 编码的,当文件
中有非 ANSCII 编码范围内的字符的时候就要使用"编码指示"来修正。
关于 sys.defaultencoding,这个在解码没有明确指明解码方式的时候使用。比如我有如下代码:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
s = '中文'
# 注意这里的 str 是 str 类型的,而不是 unicode
s.encode('gb18030')
这句代码将 s 重新编码为 gb18030 的格式,即进行 unicode -& str 的转换。因为 s 本身就是 str 类型的,因此
Python 会自动的先将 s 解码为 unicode ,然后再编码成 gb18030。因为解码是python自动进行的,我们没有指明解码方
式,python 就会使用 sys.defaultencoding 指明的方式来解码。很多情况下 sys.defaultencoding 是
ANSCII,如果 s 不是这个类型就会出错。
拿上面的情况来说,我的 sys.defaultencoding 是 anscii,而 s 的编码方式和文件的编码方式一致,是 utf8 的,所
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position
0: ordinal not in range(128)
对于这种情况,我们有两种方法来改正错误:
一是明确的指示出 s 的编码方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
s = '中文'
s.decode('utf-8').encode('gb18030')
二是更改 sys.defaultencoding 为文件的编码方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入
sys.setdefaultencoding('utf-8')
str = '中文'
str.encode('gb18030')
转载:Python操作MySQL以及中文乱码的问题
Python操作MySQL需要安装Python-MySQL
可以从网上搜索一下,和一般的Python包一样安装
安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验了一下挺好用,
不过又发现了烦人的乱麻问题,最后用了几个办法,解决了!
我用了下面几个措施,保证MySQL的输出没有乱麻:
1 Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)
2 MySQL数据库charset=utf-8
3 Python连接MySQL是加上参数 charset=utf8
4 设置Python的默认编码为 utf-8 (sys.setdefaultencoding(utf-8)
mysql_test.py
#encoding=utf-8
import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding('utf-8')
db=MySQLdb.connect(user='root',charset='utf8')
cur=db.cursor()
cur.execute('use mydb')
cur.execute('select * from mytb limit 100')
f=file("/home/user/work/tem.txt",'w')
for i in cur.fetchall():
f.write(str(i))
f.write(" ")
cur.close()
上面是linux上的脚本,windows下运行正常!
注:MySQL的配置文件设置也必须配置成utf8
设置 MySQL 的 my.cnf 文件,在 [client]/[mysqld]部分都设置默认的字符集(通常在/etc/f):
default-character-set = utf8
default-character-set = utf8
转载:实现URL编码解码的python程序
import urllib
import sys
string = sys.argv[1]
string = unicode(string,"gbk")
utf8_string = string.encode("utf-8")
gbk_string=string.encode("gbk")
gbk=urllib.quote(gbk_string)
utf8=urllib.quote(utf8_string)
print utf8
解码使用unqute和decode函数
chinakr 发短消息加为好友chinakr
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 .
传说中的长老
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 . 16#
12:54 | 只看该作者 python CGI模块获取中文编码问题解决- 部分方案
google关键字:python脚本 默认codec
来源:点击进入
今天在尝试Python的CGI模块时遇到中文字符不能正确显示的问题,很郁闷.在网上仔细找了找,终于解决了这个问题,现在将解决方法陈述如下,以防下次失误.
页面源代码如下
#-*- coding: utf8 -*-
import cgitb , cgi
cgitb.enable()
form = cgi.FieldStorage()
if (form.has_key("name") and form.has_key("addr")):
print "&p&name:", form["name"].value
print "&p&addr:", form["addr"].value
[这里仅仅测试addr参数为中文]接收Ascii字符时运行良好,但是接收中文字符时显示乱码,浏览器切换到GB2312编码时
可以正常显示,但是个人要求它成为UTF-8编码显示
改成 print "&p&addr:", form["addr"].value.encode('utf-8')
就报如下错误:
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data
在参阅了http://blog.chinaunix.net/u2/68206/showart_668359.html 后终于理解,
Python 里面的编码和解码也就是 unicode 和 str 这两种形式的相互转化。编码是 unicode -& str,相反的,解码就
是 str -& unicode。剩下的问题就是确定何时需要进行编码或者解码了.关于文件开头的"编码指示",也就是 # -*- coding: -*- 这个语句。Python 默认脚本文件都是 UTF-8 编码的,当文件中有非 UTF-8 编码范围内的字符的时候就要使用"编码指示"来修正. 关于 sys.defaultencoding,这个在解码没有明确指明解码方式的时候使用。比如我有如下代码:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
s = '中文'
# 注意这里的 str 是 str 类型的,而不是 unicode
s.encode('gb18030')
这句代码将 s 重新编码为 gb18030 的格式,即进行 unicode -& str 的转换。因为 s 本身就是 str 类型的,因此
Python 会自动的先将 s 解码为 unicode ,然后再编码成 gb18030。因为解码是python自动进行的,我们没有指明解码方式,python 就会使用 sys.defaultencoding 指明的方式来解码。很多情况下 sys.defaultencoding 是
ANSCII,如果 s 不是这个类型就会出错。拿上面的情况来说,我的 sys.defaultencoding 是 anscii,而 s 的编码方式和文件的编码方式一致,是 utf8 的,所以出错了:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position
0: ordinal not in range(128)
对于这种情况,我们有两种方法来改正错误:
一是明确的指示出 s 的编码方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
s = '中文'
s.decode('utf-8').encode('gb18030')
二是更改 sys.defaultencoding 为文件的编码方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入
sys.setdefaultencoding('utf-8')
str = '中文'
str.encode('gb18030')
看完之后,改成这样
print "&p&addr:", form["addr"].value.decode('gb2312').encode('utf-8')
我总结一下为什么要这么写的原因:
1. 当取回来的数据与你当前脚本中声明的编码不一致时就要做编码转换
2.在编码转换时首先要将该数据以自身编码的格式换成unicode码,再将这个unicode按utf8编码
3.为什么我的浏览器会传回gb2312的编码数据到服务器,这应该和客户端的系统编码有关系
这里顺便转载一下,关于Python 操作Mysql的中文问题:
Python操作MySQL以及中文乱码的问题
下面几个措施,保证MySQL的输出没有乱麻:
1 Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)
2 MySQL数据库charset=utf-8
3 Python连接MySQL是加上参数 charset=utf8
4 设置Python的默认编码为 utf-8 (sys.setdefaultencoding(utf-8)
Java代码 1.#encoding=utf-8
2.import sys
3.import MySQLdb
5.reload(sys)
6.sys.setdefaultencoding('utf-8')
8.db=MySQLdb.connect(user='root',charset='utf8')
9.cur=db.cursor()
10.cur.execute('use mydb')
11.cur.execute('select * from mytb limit 100')
13.f=file("/home/user/work/tem.txt",'w')
15.for i in cur.fetchall():
f.write(str(i))
f.write(" ")
19.f.close()
20.cur.close()
#encoding=utf-8
import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding('utf-8')
db=MySQLdb.connect(user='root',charset='utf8')
cur=db.cursor()
cur.execute('use mydb')
cur.execute('select * from mytb limit 100')
f=file("/home/user/work/tem.txt",'w')
for i in cur.fetchall():
f.write(str(i))
f.write(" ")
cur.close()
测试以下连接成功: index.psp?name=iamsese&addr=北京
--------------------------------------------------------------------------------
/yobin/blog/item/1547fbdc6ef53ba4cc116611.html
写了一个简单的脚本,将我的数据整理到Mysql中去。遇到了乱码问题,改了一下,很快就解决了。连接mysql时要注意注明是utf-8字符集,所有中文都要是utf-8,如果有GBK也要先转换成utf-8,把握这个原则,中文乱码问题是不会有的。
转换脚本如下:
Python代码 1.#-*- coding: utf-8 -*-,
2.#coding = utf-8
4.import MySQLdb,os
6.def wgdata2DB():
print "Convert weg game data to mysql"
db=MySQLdb.connect(host='localhost',
#连接数据库
user='root',
passwd='123456',
db='testdb',
charset="utf8")
#这里要注明是utf8字符集,文件开头最好也加上utf-8的声明
cursor=db.cursor()
if os.path.exists('test.dat'):
rFile = open('test.dat', 'r')
lines = rFile.readlines()
rFile.close()
for line in lines:
print "handle line:%d" % (loop)
myset = line.split(' ')
sqlstr = "INSERT INTO wg_Content (type,title,url,speed,des,size) VALUES('%s','%s','%s','%s','%s','%s')" \
%(myset[0],myset[1],myset[2],myset[3],myset[4],myset[5])
cursor.execute(sqlstr)
db.commit()
cursor.close()
db.close()
#-*- coding: utf-8 -*-,
#coding = utf-8
import MySQLdb,os
def wgdata2DB():
print "Convert weg game data to mysql"
db=MySQLdb.connect(host='localhost',
#连接数据库
user='root',
passwd='123456',
db='testdb',
charset="utf8")
#这里要注明是utf8字符集,文件开头最好也加上utf-8的声明
cursor=db.cursor()
if os.path.exists('test.dat'):
rFile = open('test.dat', 'r')
lines = rFile.readlines()
rFile.close()
for line in lines:
print "handle line:%d" % (loop)
myset = line.split(' ')
sqlstr = "INSERT INTO wg_Content (type,title,url,speed,des,size) VALUES('%s','%s','%s','%s','%s','%s')" \
%(myset[0],myset[1],myset[2],myset[3],myset[4],myset[5])
cursor.execute(sqlstr)
db.commit()
cursor.close()
db.close()
1 楼 vb2005xu
Python操作MySQL需要安装Python-MySQL
1 Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)
2 MySQL数据库charset=utf-8
3 Python连接MySQL是加上参数 charset=utf8
4 设置Python的默认编码为 utf-8 (sys.setdefaultencoding(utf-8)
Java代码 1.#encoding=utf-8
2.import sys
3.import MySQLdb
5.reload(sys)
6.sys.setdefaultencoding('utf-8')
8.db=MySQLdb.connect(user='root',charset='utf8')
9.cur=db.cursor()
10.cur.execute('use mydb')
11.cur.execute('select * from mytb limit 100')
13.f=file("/home/user/work/tem.txt",'w')
15.for i in cur.fetchall():
f.write(str(i))
f.write(" ")
19.f.close()
20.cur.close()
#encoding=utf-8
import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding('utf-8')
db=MySQLdb.connect(user='root',charset='utf8')
cur=db.cursor()
cur.execute('use mydb')
cur.execute('select * from mytb limit 100')
f=file("/home/user/work/tem.txt",'w')
for i in cur.fetchall():
f.write(str(i))
f.write(" ")
cur.close()
推荐使用SQLObject,我在写测试代码时,在SQLITE中没有出现这个问题,因为turbogears缺省类库都是unicode编码的
chinakr 发短消息加为好友chinakr
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 .
传说中的长老
帖子34321 精华364 积分701328 在线时间5119 小时 注册时间 . 17#
03:01 | 只看该作者 再论字符集
google关键字:UnicodeDecodeError: 'gb18030' codec can't decode bytes in position
来源:点击进入
作者:kino 日期: 13:46:46
对应中国人来说字符集的相互转换真是麻烦,搞不好就是大串的乱码,实在有必要多多复习一下基本概念!!
ISO8859-1,通常叫做Latin-1。Latin-1包括了书写所有西方欧洲语言不可缺少的附加字符。而gb2312是标准中文字符集。
UTF-8 是 UNICODE 的一种变长字符编码,即 RFC 3629。简单的说——大字符集。可以解决多种语言文本显示问题,从而实现应用国际化和本地化。
对系统来讲,UTF-8 编码可以通过屏蔽位和移位操作快速读写,排序更加容易。UTF-8 是字节顺序无关的,它的字节顺序在所有系统中都是一样的。因此 UTF-8 具有更高的性能。
在chinaunix看到篇实例讲解,很是直观,下面就贴出来,与大家共享!!
1 python 代码
1. &&& a = "我"
2. &&& b = unicode(a,"gb2312")
3. &&& a.__class__
4. &type 'str'&
5. &&& b.__class__
6. &type 'unicode'&
看出来了吧,两种字符串。再来python 代码
2. '\xce\xd2'
4. u'\u6211'
变量a是两个字符,b是一个unicode字符。关于这两种字符串,Python文档--&LanguageReference--&DataModel--&The standard type hierarchy--&Sequences,有一些Strings,Unicode的描述。至于python 代码
1. &&& z = u"我"
2. &&& #这种代码,其实什么都不是。
3. &&& z.__class__
4. &type 'unicode'&
6. u'\xce\xd2'
看到了吧,这个奇怪的东西......后来在WindowsXP、纯python命令行下试过,得出的结论不同,z的结果变成了u'\u6211',这里完全不应该在pyshell下作试验的,看来还有很多问题尚未理解清楚 再来看看encode,decode什么情况用encode,什么情况又是decode呢,刚开始总是被搞昏。其实各种本地字符集的英文名是Coded Character Set,要转换为Coded,肯定是要encode了,同样,从里面解出来也应该叫decode…… decode就是把其他编码转换为unicode,等同于unicode函数;encode就是把unicode编码的字符串转换为特定编码。在pyshell里继续:a是Str类型的,所以再用encode会报错。用print输出时会调用默认编码转换为系统编码? python 代码
1. &&& a.decode("gb2312")
2. u'\u6211'
3. &&& print a.decode("gb2312")
5. &&& a.encode("gb2312")
6. Traceback (most recent call last):
File "&input&", line 1, in ?
8. UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 0: ordinal not in range(128)
b是unicode类型,打印时需要先encode(编码)成系统编码python 代码
1. &&& print b.encode("gb2312")
3. &&& b.encode("gb2312")
4. '\xce\xd2'
5. &&& b.decode("gb2312")
6. Traceback (most recent call last):
File "&input&", line 1, in ?
8. UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128
字符串内码的转换,是开发中经常遇到的问题。
在Java中,我们可以先对某个String调用getByte(),由结果生成新String的办法来转码,也可以用NIO包里面的Charset来实现。
在Python中,可以对String调用decode和encode方法来实现转码。
比如,若要将某个String对象s从gbk内码转换为UTF-8,可以如下操作
s.decode('gbk').encode('utf-8')
可是,在实际开发中,我发现,这种办法经常会出现异常:
UnicodeDecodeError: 'gbk' codec can't decode bytes in position : illegal multibyte sequence
这是因为遇到了非法字符——尤其是在某些用C/C++编写的程序中,全角空格往往有多种不同的实现方式,比如\xa3\xa0,或者\xa4\x57,这些字符,看起来都是全角空格,但它们并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在转码的过程中出现了异常。
这样的问题很让人头疼,因为只要字符串中出现了一个非法字符,整个字符串——有时候,就是整篇文章——就都无法转码。
幸运的是,tiny找到了完美的解决办法(我因此被批评看文档不仔细,汗啊……)
s.decode('gbk', 'ignore').encode('utf-8')
因为decode的函数原型是decode([encoding], [errors='strict']),可以用第二个参数控制错误处理的策略,默认的参数就是strict,代表遇到非法字符时抛出异常;
如果设置为ignore,则会忽略非法字符;
如果设置为replace,则会用?取代非法字符;
如果设置为xmlcharrefreplace,则使用XML的字符引用。
原文链接:http://blog.chinaunix.net/uid--id-4074412.html
阅读: 1744 |}

我要回帖

更多关于 python mysql 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信