python - 如何在没有逗号的情况下将多个值添加到字典中的单个键中?
问题描述
在我不久前提出的一个问题中,我尝试使用 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'
推荐阅读
- reactjs - JSX Return 中的表达式给出“意外的令牌,预期的”,“”
- javascript - Django将网站上传到heroku后出现ajax错误
- javascript - Chrome 扩展 - 打开窗口并控制它
- r - 调整栅格坐标
- php - 使用 Discord API 和 cURL 发送 Discord DM
- sql - 为什么'NOT IN'有效,但'NOT EXISTS'无效?
- reactjs - 我在使用 DataGrid Material UI 组件时遇到错误,它显示“无法解析 '@material-ui/x-license'”,为什么?
- amazon-web-services - AWS CodeDeploy Appspec.yml 文件未复制到目标
- android - 如何处理协程中的线程执行?
- python - 如何使右括号“)”与python中的打印行一致?