繁体   English   中英

如何在没有逗号的情况下将多个值添加到字典中的单个键中?

[英]How can you add multiple values into a single key in a dictionary without it having commas?

在我刚才问的一个问题中,我必须尝试 PyInquirer 一次编辑 .txt 文件的多行。 当我发现问题时,我尝试摆弄它。

这是代码:


question = [
    {
        'type': 'input',
        'name': 'text_lines',
        'message': '',
        'default': ('This is a test line\n' 'This is another test line\n')
    }
]

answer = prompt(question)
print('The file says:\n {}'.format(answer['text_line']))

如您所见,键'default'使用括号,但字符串之间没有逗号。 当我在它们之间放置逗号时,会出现错误TypeError: not all arguments converted during string formatting 我怎样才能把两个字符串放在同一个键上??? 这让我感到困惑,因为即使使用方括号和大括号也不起作用。

我只想重新创建上面没有逗号的'default'

编辑:我没有意识到这只是串联。 谢谢你的回答!!

由于没有逗号,因此它不是元组:它是一个表达式评估,并且该表达式是两个字符串的连接。 在交互模式下尝试:

>>> ('This is a test line\n' 'This is another test line\n')
'This is a test line\nThis is another test line\n'

暂无
暂无

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

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