我问过A python program fails to execute in sublime text 3, but success in bash。我做了一些研究,发现有必要开始一个新问题。
在python2.7中,<!DOCTYPE html>
<html lang="en" ng-app="demo">
<head>
<meta charset="UTF-8">
<title>Hourlies</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.2/angular.min.js">
</script>
<script src="https://code.angularjs.org/1.5.0-beta.0/angular-route.min.js"></script>
<style>
.container{
margin-top: 10%;
background-color: rgb(245,245,245);
}
.wi{
width: 270px;
height: 300px;
border:3px solid black;
margin-top: 1%;
}
</style>
</head>
<body>
<div class="container">
<div class="col-lg-3">
</div>
<!-- end of col-3 -->
<div class="col-lg-9">
<div ng-controller="myCtrl">
<select
ng-model="selectedItem"
ng-options="S.value as S.label for S in selectables" ng-change="fun()">
</select>
<br><br>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
<img src='{{selectedItem}}' class="wi"/>
</div>
<!-- end of col-9 -->
</div>
<!-- enf of main container -->
<script>
var app=angular.module('demo',['ngRoute']);
app.controller('myCtrl', function($scope) {
$scope.selectables = [
{
label: ' vallay',
value: 'http://www.quackit.com/pix/routeburn_track/routeburn_flats_t.jpg',
vlue:'http://www.freeimageslive.com/galleries/transtech/informationtechnology/preview/blue_screen.jpg',
value:'http://www.freeimageslive.com/galleries/transtech/informationtechnology/preview/chiclet_keyboard.jpg'
},
{ label: 'Tejas',
value: 'http://upload.wikimedia.org/wikipedia/commons/a/af/Bonsai_IMG_6426.jpg'
}
];
});
</script>
</body>
</html>
是ascii:
sys.getdefaultencoding()
在我看来,打印obj等于In [1]: import sys
In [2]: sys.getdefaultencoding()
Out[2]: 'ascii'
。如果print str(obj)
是unicode,它将被编码为ascii。例如(test.py):
obj
但是ipython中没有出现错误:
#-*- encoding:utf-8 -*-
import sys
print sys.getdefaultencoding() # ascii
print "你好"
print u"你好" # should be an error occured: UnicodeEncodeError: 'ascii' codec...
为什么在ipython中打印unicode obj没有出错?我的理解不对吗?
答案 0 :(得分:2)
打印时,仅当Python无法确定终端编码时才使用默认编码。两者都适用于您的情况,因为第一个"你好"
是已经在终端编码中编码的字节字符串。第二个u"你好"
是一个Unicode字符串,将以终端编码进行编码,前一个命令已经显示支持中文。
ascii
。例如,语句u'你好'.decode('utf8')
是一个常见错误,其中在Unicode字符串上调用.decode()
,但只有字节字符串具有.decode()
方法。然后,Python尝试使用默认的ascii
编解码器将Unicode字符串编码为字节字符串,以便可以调用.decode()
。
下面的示例,但请注意它是编码错误,而不是解码错误:
>>> u'你好'.decode('utf8')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "d:\dev\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)