如何在Python 2.7中将输入从cmd转换为unicode

时间:2016-04-25 18:45:27

标签: python character-encoding

我有这样的输入声明。

# -*- coding: utf-8 -*-
myentry = raw_input("Enter a letter").decode("iso-8859-1").encode("utf-8")

if myentry == "ö":
    print(myentry)
if myentry == "ä":
    print("ääää")

问题是当我输入ö或ä时没有任何反应,程序终止。

我在Windows 7下使用cmd。我将脚本保存为utf-8

2 个答案:

答案 0 :(得分:0)

如果你一直在使用python 2.x,你可以尝试声称字母是unicode符号,所以它是这样的:

```{r,echo = FALSE, warning = FALSE, message=FALSE, error=TRUE}
knitr::opts_chunk$set(cache=TRUE)

#MatrixPos - not empty
MatrixPos <- structure(list(difFwd1 = 0, difFwd2 = 0.200000000000045, difFwd3 = 0.100000000000136,difFwd4 = 0, difFwd5 = 0.200000000000045), .Names = c("difFwd1","difFwd2", "difFwd3", "difFwd4", "difFwd5"), row.names = "155", class = "data.frame") 

#MatrixNeg - empty
MatrixNeg <- structure(list(difFwd1 = NA_real_, difFwd2 = NA_real_, difFwd3 = NA_real_,difFwd4 = NA_real_, difFwd5 = NA_real_), .Names = c("difFwd1","difFwd2", "difFwd3", "difFwd4", "difFwd5"), row.names = "NA", class = "data.frame") 

boxplot(MatrixPos, notch = TRUE, outline = TRUE)
boxplot(MatrixNeg, notch = TRUE, outline = TRUE)

```

在python 3.x中也应该有这样的东西

答案 1 :(得分:0)

您的默认编码可能设置为ProjectA |---module1 |---module2 sonar.modules=module1, module2 module1.sonar.projectBaseDir=module1 module2.sonar.projectBaseDir=module2 。您可以将其更改为ascii,如下所示:

utf-8

启动程序后,import sys reload(sys) sys.setdefaultencoding("utf-8") 功能将被删除。重新加载setdefaultencoding()会将其恢复,以便您可以更改编码。所以这是一种破解,可能不是解决这个问题的理想方式。现在你应该能够像这样阅读你的输入:

sys

这对我有用(在Ubuntu上使用Python 2.7)