Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想用双引号替换列表中的单引号_Python_String_List_Python 3.x - Fatal编程技术网

Python 我想用双引号替换列表中的单引号

Python 我想用双引号替换列表中的单引号,python,string,list,python-3.x,Python,String,List,Python 3.x,所以我正在制作一个程序,它接收一个文本文件,将其分解成单词,然后将列表写入一个新的文本文件 我遇到的问题是,我需要列表中的字符串使用双引号而不是单引号 比如说 当我想要这个[“狗”,“猫”,“鱼”]时,我得到了这个[“狗”,“猫”,“鱼”] 这是我的密码 with open('input.txt') as f: file = f.readlines() nonewline = [] for x in file: nonewline.append(x[:-1]) words = [

所以我正在制作一个程序,它接收一个文本文件,将其分解成单词,然后将列表写入一个新的文本文件


我遇到的问题是,我需要列表中的字符串使用双引号而不是单引号

比如说

当我想要这个
[“狗”,“猫”,“鱼”]
时,我得到了这个
[“狗”,“猫”,“鱼”]

这是我的密码

with open('input.txt') as f:
    file = f.readlines()
nonewline = []
for x in file:
    nonewline.append(x[:-1])
words = []
for x in nonewline:
    words = words + x.split()
textfile = open('output.txt','w')
textfile.write(str(words))
我是python新手,还没有发现任何关于这方面的信息。 有人知道怎么解决这个问题吗


[编辑:我忘了提到我在arduino项目中使用了输出,该项目要求列表具有双引号。]

您不能更改
str
list
的工作方式

对于字符串使用哪个use
如何

>>> animals = ['dog','cat','fish']
>>> print(str(animals))
['dog', 'cat', 'fish']

>>> import json
>>> print(json.dumps(animals))
["dog", "cat", "fish"]


在Python中,双引号和单引号是相同的。它们之间没有区别。用双引号替换单引号没有意义,反之亦然:

2.4.1.字符串和字节文本

…通俗易懂:两种类型的文字都可以用匹配的单引号(')或双引号(“)括起来。它们也可以包含在三个单引号或双引号(通常称为三引号字符串)的匹配组中。反斜杠()字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符


“我遇到的问题是,我需要列表中的字符串使用双引号而不是单引号。”-然后,您需要让程序接受单引号,而不是尝试用双引号替换单引号。

最有可能的情况是,您只需在输出中将单引号替换为双引号,方法是替换它们:

str(words).replace("'", '"')
您还可以扩展Python的
str
类型,并使用新类型包装字符串,将
\uuuu repr\uuu()
方法更改为使用双引号而不是单引号。不过,使用上面的代码最好更简单、更明确

class str2(str):
    def __repr__(self):
        # Allow str.__repr__() to do the hard work, then
        # remove the outer two characters, single quotes,
        # and replace them with double quotes.
        return ''.join(('"', super().__repr__()[1:-1], '"'))

>>> "apple"
'apple'
>>> class str2(str):
...     def __repr__(self):
...         return ''.join(('"', super().__repr__()[1:-1], '"'))
...
>>> str2("apple")
"apple"
>>> str2('apple')
"apple"

['dog','cat','fish']与[dog,cat,fish]相同,引号只是用来将字符串正弦化。引号不是字符串的一部分。试着打印(单词[0]),你就会明白“我遇到的问题是我需要列表中的字符串使用双引号而不是单引号。”-为什么?对于这个需要双引号的文件,您将如何处理?你是想输出JSON还是别的什么?(如果是这样的话,那么就有一个简单的例子。)请显示输入文件。
json.dumps('dog')
outputs
“dog”
到文件中,但我想要
“dog”
。怎么做?@chandresh,打印出来。周围的单引号表示对象为字符串。(这就是
repr
的表示方式)。如果你把它打印出来(或写进文件,…),你会得到你想要的。@falsetru我这样做了,只是在那之后我才把我的评论贴在上面。你能再检查一遍吗?@chandresh,你能不能再发一个问题,让我知道这个问题的url。如果没有可复制的代码,很难知道问题出在哪里。@falsetru这是不正确的。尽管Python没有区别,但PostgreSQL就是一个例子。如果将单引号字符串列名定义为双引号列名,则传递单引号字符串列名将返回错误。因此,有时仍有必要替换引号。
class str2(str):
    def __repr__(self):
        # Allow str.__repr__() to do the hard work, then
        # remove the outer two characters, single quotes,
        # and replace them with double quotes.
        return ''.join(('"', super().__repr__()[1:-1], '"'))

>>> "apple"
'apple'
>>> class str2(str):
...     def __repr__(self):
...         return ''.join(('"', super().__repr__()[1:-1], '"'))
...
>>> str2("apple")
"apple"
>>> str2('apple')
"apple"