简体   繁体   English

如何在python2中打印出由双引号表示的大小为n的空字符串列表

[英]How to print out a list of empty strings of size n represented by double quotes in python2

How to print out a list of empty strings of size n while the empty strings are represented by double quotes "" not single quotes '' ( ["", "", ""] ) in python2?如何打印出大小为 n 的空字符串列表,而空字符串在 python2 中由双引号""而不是单引号'' ( ["", "", ""] ) 表示?

I want to initialize a list of ["", "", "", ""] not a list of ['', '', '', ''] of a given size n, it has nothing to do with JSON format.我想初始化一个["", "", "", ""]列表而不是给定大小 n 的['', '', '', ''] ,它与 JSON 无关格式。

And why does python automatically convert all the double quotes into single quotes?为什么python会自动把所有的双引号都转成单引号呢?
ie print ["abc"] will print out ['abc'] and print [""] will print out ['']即 print ["abc"]将打印出['abc']并且 print [""]将打印出['']

So far I can only get single quotes printed out [''] but the output I'm seeking strictly requires double quotes [""] inside and how to do that?到目前为止,我只能打印出单引号['']但我正在寻找的输出严格要求在内部使用双引号[""]以及如何做到这一点?

print [""] will print out [''] print [""]将打印出['']

print [`""`] will print out ["''"] print [`""`]将打印出["''"]

print ['""'] will print out ['""'] print ['""']将打印出['""']

How you represent your data in code is not part of that data itself .在代码中如何表示数据不是数据本身的一部分 That is, "foo" and 'foo' and r'foo' are all the exact same string .也就是说, "foo"'foo'r'foo'都是完全相同的 string There's no semantic difference between them (after parsing, the way they're stored in memory is identical), so they get printed the same way.它们之间没有语义差异(解析后,它们在内存中的存储方式是相同的),因此它们的打印方式相同。

If you want to print something in JSON syntax (and double quotes are the only type valid for strings in JSON), use json.dumps() to generate a JSON encoding of your list (and its enclosed string).如果您想以 JSON 语法打印某些内容(并且双引号是唯一对 JSON 中的字符串有效的类型),请使用json.dumps()生成列表(及其包含的字符串)的 JSON 编码。 This will necessarily use double quotes, because if it didn't use double quotes, the result would not be valid JSON.这必然会使用双引号,因为如果使用双引号,结果将不是有效的 JSON。

That is:那是:

import json

a = ['']
print(json.dumps(a))

...will have your intended result. ......会有你想要的结果。

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

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