簡體   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