无法将度数符号转换为raw_input [英] Can't get a degree symbol into raw_input

查看:167
本文介绍了无法将度数符号转换为raw_input的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中的问题看起来像这样:

The problem in my code looks something like this:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

deg = u'°'
print deg
print '40%s N, 100%s W' % (deg, deg)
codelim = raw_input('40%s N, 100%s W)? ' % (deg, deg))



我试图生成一个raw_input提示符纬度/经度字符串,并且提示应包括此类字符串的示例。 print deg print '40%s N,100%s W'%(deg,deg) - 它们分别返回°和40°N,100°W - 但是 raw_input 步骤每次都失败。我得到的错误如下:

I'm trying to generate a raw_input prompt for delimiter characters inside a latitude/longitude string, and the prompt should include an example of such a string. print deg and print '40%s N, 100%s W' % (deg, deg) both work fine -- they return "°" and "40° N, 100° W" respectively -- but the raw_input step fails every time. The error I get is as follows:

Traceback (most recent call last):
  File "C:\Users\[rest of the path]\scratch.py", line 5, in  <module>
    x = raw_input(' %s W")? ' % (deg))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 1:
ordinal not in range(128)

我想我已经通过添加编码头解决了问题,按照此处(确实有可能打印所有的度数符号),但是我仍然得到Unicode错误,只要我添加一个否则安全的字符串到 raw_input

I thought I'd have solved the problem by adding the encoding header, as instructed here (and indeed that did make it possible to print the degree sign at all), but I'm still getting Unicode errors as soon as I add an otherwise-safe string to raw_input. What's going on here?

推荐答案

尝试将提示字符串编码为 stdout

Try encoding the prompt string to stdouts encoding before passing it to raw input

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys

deg = u'°'

prompt = u'40%s N, 100%s W)? ' % (deg, deg)
codelim = raw_input(prompt.encode(sys.stdout.encoding))




40°N,100°W)?

40° N, 100° W)?

这篇关于无法将度数符号转换为raw_input的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆