Python 2.7 - IPython'raw_input'并附加到列表 - 在每个项目之前添加'u' [英] Python 2.7 - IPython 'raw_input' and appending to a list - adds 'u' before each item

查看:100
本文介绍了Python 2.7 - IPython'raw_input'并附加到列表 - 在每个项目之前添加'u'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OSX Lion上使用 Python 2.7 。我正在使用 IPython ,其中 Pandas 0.11.0 Numpy Statsmodels 包。

I'm using Python 2.7 on Mac OSX Lion. I'm using IPython with the Pandas 0.11.0, Numpy and Statsmodels packages.

我正在写一个允许用户对a进行逻辑回归的函数file,指定在构建模型时使用哪些变量,哪些变量应转换为虚拟变量,哪个变量应该是自变量。

I'm writing a function that allows the user to do logistic regression on a file, specifying which variables to be used in building the model, which should be transformed into dummy variables and which variable should be the independent variable.

当我执行以下操作时,例如:

When I do the following, for instance:

 cols_to_keep = []
 print (df.columns)
 i = eval(raw_input('How many of these variables would you like to use in logistic regression?: '))
 while i != 0:
    i = i - 1
    print (df.columns)
    addTo = raw_input('Enter a variable for this list that you would like to keep and use in logistic regression.: ')
    cols_to_keep.append(addTo)

我最终遇到了问题。特别是当我要求用户从列表中指定因变量,然后需要从训练变量列表中取出该变量时:

I end up running into problems down the road. Specifically when I ask the user to specify the dependent variable from a list and then need to take that variable out of the list of training variables:

print (df.columns)

dependent = raw_input('Which of these columns would you like to be the dependent variable?: ')
training.remove(dependent)

我在插入print语句后发现,添加到训练变量列表中的变量看起来像这样:

I found, after inserting a print statement, that the variables added to the list of training variables, looks like this:

('these are the traing variables: ', ['access', u'age_age6574', u'age_age75plus', u'sex_male', u'stage_late', u'death_death'])

看来a u 已经放在每个用户指定的变量之前。

It appears that a u has been placed before each user-specified variable.

我的问题是:为什么这样以及如何修复/解决这个问题,以便当用户指定因变量时,它实际上已从列表中删除。这也发生在用户指定变量并将其添加到列表的所有其他实例中,如果我需要用户观察列表,则会产生混淆。

My question is: why is this and how do fix/get around this issue so that, when the user specifies the dependent variable, it is actually removed from the list. This also occurs in all other instances where a user specifies a variable and it is added to a list, creating confusion if I ever need the user to observe the list.

推荐答案

这些只是unicode字符串,而不是字节字符串。没有错,字符串的内容不受影响。 u'text'只是为了让你在查看repr时能够区分Python 2中的字节字符串和unicode字符串。如果您打印字符串,您将看到没有区别。这在Python 3中是相反的,其中text表示unicode字符串,而 bbytes表示一个字节字符串。

Those are just unicode strings, as opposed the byte strings. There is nothing wrong, and the content of the string is not affected. The u'text' is just so that you can tell the difference between byte strings and unicode strings in Python 2 when you look at the repr. If you print the string, you will see no difference. This is reversed in Python 3, where "text" means a unicode string, while b"bytes" means a byte string.

如果你真的想将它们强制为字节串(不太可能),你可以这样做:

If you really want to coerce them to bytestrings (unlikely), you can do:

def ensure_str(s):
    if isinstance(s, unicode):
        s = s.encode('utf-8')
    return s

s = ensure_str(raw_input("prompt >"))

这篇关于Python 2.7 - IPython'raw_input'并附加到列表 - 在每个项目之前添加'u'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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