Python 我想用列表中的双引号替换单引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42183479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to replace single quotes with double quotes in a list
提问by ThatsOkay
So I am making a program that takes a text file, breaks it into words, then writes the list to a new text file.
所以我正在制作一个程序,它接受一个文本文件,将它分解成单词,然后将列表写入一个新的文本文件。
The issue I am having is I need the strings in the list to be with double quotes not single quotes.
我遇到的问题是我需要列表中的字符串使用双引号而不是单引号。
For example
例如
I get this ['dog','cat','fish']
when I want this ["dog","cat","fish"]
['dog','cat','fish']
当我想要这个的时候我得到这个["dog","cat","fish"]
Here is my code
这是我的代码
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))
I am new to python and haven't found anything about this. Anyone know how to solve this?
我是 python 的新手,还没有发现任何关于这个的东西。有谁知道如何解决这个问题?
[Edit: I forgot to mention that i was using the output in an arduino project that required the list to have double quotes.]
[编辑:我忘了提到我在 arduino 项目中使用输出,该项目要求列表带有双引号。]
回答by falsetru
You cannot change how str
works for list
.
你不能改变如何str
进行工作的list
。
How about using JSON formatwhich use "
for strings.
如何使用用于字符串的JSON 格式"
。
>>> animals = ['dog','cat','fish']
>>> print(str(animals))
['dog', 'cat', 'fish']
>>> import json
>>> print(json.dumps(animals))
["dog", "cat", "fish"]
import json
...
textfile.write(json.dumps(words))
回答by Harvey
Most likely you'll want to just replace the single quotes with double quotes in your output by replacing them:
很可能您只想通过替换输出中的双引号来替换单引号:
str(words).replace("'", '"')
You couldalso extend Python's str
type and wrap your strings with the new type changing the __repr__()
method to use double quotes instead of single. It's better to be simpler and more explicit with the code above, though.
您可能还扩展Python的str
类型和新的类型改变包装你的字符串__repr__()
使用双引号,而不是单一的方法。不过,最好使用上面的代码更简单、更明确。
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"
回答by Huy Vo
In Python, double quote and single quote are the same. There's no different between them. And there's no point to replace a single quote with a double quote and vice versa:
在 Python 中,双引号和单引号是一样的。他们之间没有什么不同。用双引号替换单引号是没有意义的,反之亦然:
2.4.1. String and Bytes literals
...In plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character...
2.4.1. 字符串和字节文字
...用简单的英语:两种类型的文字都可以用匹配的单引号 (') 或双引号 (") 括起来。它们也可以括在由三个单引号或双引号组成的匹配组中(这些通常称为三重-quoted strings). 反斜杠 () 字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符...
"The issue I am having is I need the strings in the list to be with double quotes not single quotes." - Then you need to make your program accept single quotes, not trying to replace single quotes with double quotes.
“我遇到的问题是我需要列表中的字符串使用双引号而不是单引号。” - 然后你需要让你的程序接受单引号,而不是试图用双引号替换单引号。