简体   繁体   English

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

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

In a question I asked a while ago, I got to try PyInquirer to edit multiple lines of a .txt file at once.在我刚才问的一个问题中,我必须尝试 PyInquirer 一次编辑 .txt 文件的多行。 I tried fiddling with it, when I found a problem.当我发现问题时,我尝试摆弄它。

This is the code:这是代码:


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']))

As you can see, the key 'default' used parentheses but there are no commas between the strings.如您所见,键'default'使用括号,但字符串之间没有逗号。 When I put commas between them, the error TypeError: not all arguments converted during string formatting occurs.当我在它们之间放置逗号时,会出现错误TypeError: not all arguments converted during string formatting How can I put two strings with the same key???我怎样才能把两个字符串放在同一个键上??? It was confusing to me because even the usage of square and curly brackets don't work.这让我感到困惑,因为即使使用方括号和大括号也不起作用。

I just want to recreate the 'default' key above without commas我只想重新创建上面没有逗号的'default'

Edit: I didn't realize it was just concatenation.编辑:我没有意识到这只是串联。 Thanks for your answers!!谢谢你的回答!!

Since there are no commas, that is not a tuple: it's an expression evaluation, and the expression is the concatenation of two strings.由于没有逗号,因此它不是元组:它是一个表达式评估,并且该表达式是两个字符串的连接。 Try it in interactive mode:在交互模式下尝试:

>>> ('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