简体   繁体   English

python-使用raw_input列出字典

[英]python - list a dictionary using raw_input

I'm using the following line to make a question with options from a dictionary 我正在使用以下行用字典中的选项提出问题

int(raw_input("Please select one of the following options %s: " % str(dict_options).strip('{}')))

This is the output, for example: 这是输出,例如:

Please select one of the following options: 1: 'Development', 2: 'Certification', 3: 'Production'

This is the output I need: 这是我需要的输出:

Please select one of the following options:
1) Development
2) Certification
3) Production

There anyway to change the raw_input line? 无论如何要更改raw_input行? For some reason PyCharm need to put a text on the raw_input() function. 由于某些原因,PyCharm需要在raw_input()函数上放置文本。

You can replace the commas with newline characters by doing .replace(',', '\\n') . 您可以通过执行.replace(',', '\\n')用换行符替换逗号。 The code looks something like: 该代码如下所示:

dict_options = {1: 'Development', 2: 'Certification', 3: 'Production'}
print " " + str(dict_options).strip('{}').replace(',', '\n')

OUTPUT 输出值

 1: 'Development'
 2: 'Certification'
 3: 'Production'

The first space' purpose is to align the options. 第一个空格的目的是对齐选项。

If you want to replace the colon with a ) you can add an additional replace: .replace(':', ')') 如果你想更换一个冒号) ,你可以添加额外的替换: .replace(':', ')')

If you want to get rid of the quotes you can do an additional: .replace("'", '') 如果要删除引号,可以执行其他操作: .replace("'", '')

etc. 等等

Example

dict_options = {1: 'Development', 2: 'Certification', 3: 'Production'}

print " " + str(dict_options).strip('{}') \
                    .replace(',', '\n') \
                    .replace(':', ')') \
                    .replace("'", '')

OUTPUT 输出值

 1) Development
 2) Certification
 3) Production

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM